我有一个很长的数组,如下所示。我需要实现的是检查是否在这个数组中的每6个元素中找到值1。如果没有找到,必须随机添加到6个元素中的一个(通过替换任何原始值)。此外,如果每6个元素找到该值超过2次,则需要将其中一个替换为另一个随机数。
我已经创建了能够随机生成以下数组的代码。但我不知道如何检查数组中每6个元素是否包含值1,如果不是如何随机添加它,如果发现超过1次如何用另一个随机数替换其中一个值。
我知道这很难理解。我希望有人能帮上忙。
简而言之,最终结果应该是:
每6个元素,这个数组必须包含至少一次且不超过一次的值1,位于随机位置的每6个元素。
这就是我到目前为止所做的,这段代码生成了下面的数组:
/*
We select the elements to add randomly in the final array from a DB table
*/
$elements = parent::$db->select('_matrix_elements', 'element_id', NULL, 'ORDER BY RAND()');
$count = count($elements) - 1; //count the total number of the elements -1 to start from zero
$columns = 6;
$lines = 8;
//generating the final array
for ($a = 1; $a <= $lines; $a++) {
for ($b = 1; $b <= $columns; $b++) {
$rand = rand(1,$count);
$line['position_'.$a.'_' . $b] = $elements[$rand]['element_id'];
}
}
Array
(
[position_1_1] => 14
[position_1_2] => 6
[position_1_3] => 5
[position_1_4] => 6
[position_1_5] => 9
[position_1_6] => 8
[position_2_1] => 11
[position_2_2] => 7
[position_2_3] => 6
[position_2_4] => 1
[position_2_5] => 7
[position_2_6] => 5
[position_3_1] => 14
[position_3_2] => 5
[position_3_3] => 4
[position_3_4] => 7
[position_3_5] => 4
[position_3_6] => 10
[position_4_1] => 6
[position_4_2] => 2
[position_4_3] => 2
[position_4_4] => 1
[position_4_5] => 7
[position_4_6] => 6
[position_5_1] => 3
[position_5_2] => 7
[position_5_3] => 8
[position_5_4] => 10
[position_5_5] => 3
[position_5_6] => 2
[position_6_1] => 8
[position_6_2] => 2
[position_6_3] => 10
[position_6_4] => 2
[position_6_5] => 10
[position_6_6] => 9
[position_7_1] => 6
[position_7_2] => 10
[position_7_3] => 4
[position_7_4] => 8
[position_7_5] => 1
[position_7_6] => 5
[position_8_1] => 2
[position_8_2] => 7
[position_8_3] => 4
[position_8_4] => 7
[position_8_5] => 9
[position_8_6] => 13
)
发布于 2013-07-26 04:53:47
$list_2d = array_chunk($list,6);$final_list =数组();foreach ($list_2d as $array) { $count = array_count_values( $array);if (!array_key_exists(1,$count)) $arraymt_rand(0,5) = 1;else if ($count1 > 1) for ($i = 1;$i <= $count1 - 1;$i++) (1,)=(2,15);//这里可以任意使用随机数,只要不是1 $final_list = array_merge($final_list,$array);}
发布于 2013-07-26 04:58:41
最好是马上解决它,而不是制定另一个解决方案来修复它。在您的代码中尝试这两个函数。另外,我还测试了它,所以请在这里查看http://phpfiddle.org/lite/code/i9e-gb3。希望这就是你需要的。
function randomizer($columns, $rows, $elements)
{
$line = array();
for ($row = 1; $row <= $rows; $row++)
{
$one_count = array();
for ($col = 1; $col <= $columns; $col++)
{
$line['position_' . $row . '_' . $col] = randomID($elements);
if ($line['position_' . $row . '_' . $col] == 1)
{
//we have 1 - store key value
$one_count[] = 'position_' . $row . '_' . $col;
}
}
if (empty($one_count))
{
//no 1 in last row - we will replace one random
$rand_col = rand(1, $columns);
$line['position_' . $row . '_' . $rand_col] = 1;
}
elseif (count($one_count) > 1)
{
//more than one 1 in last row, we will leave only one
//first - pick one random to keep
$keep_key = array_rand($one_count);
unset($one_count[$keep_key]);
// second - replace others with non 1 values
foreach ($one_count as $repl_key)
{
//this time we won't take ID=1
$line[$repl_key] = randomID($elements, true);
}
}
}
return $line;
}
function randomID($elements, $not1=false)
{
$count = count($elements) - 1;
$rand = rand(0, $count);
$el_id = $elements[$rand]['element_id'];
if ($not1 === true && $el_id == 1)
{
return randomID($elements, true);
}
return $el_id;
}
几乎忘记了,关于这个$rand = rand(0, $count);
<这是正确的方式。您已经完成了$count = count($elements) - 1;
,但仍从1
启动了rand :)
https://stackoverflow.com/questions/17867509
复制相似问题