我有一个如下所示的长数组。 我需要做的是检查此数组中的每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
)
答案 0 :(得分:1)
$list_2d = array_chunk($list, 6); $final_list = array(); foreach ($list_2d as $array) { $count = array_count_values($array); if (!array_key_exists(1, $count)) $array[mt_rand(0, 5)] = 1; else if ($count[1] > 1) for ($i = 1; $i <= $count[1] - 1; $i++) $array[array_search(1, $array)] = mt_rand(2, 15); //Use whatever random number you want here, as long as it's not 1 $final_list = array_merge($final_list, $array); }
答案 1 :(得分:0)
最好立即做好,而不是制定另一种解决方案。用你的代码尝试这两个函数。我也测试了它,所以在这里查看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);
&lt;这是正确的方法。您已完成$count = count($elements) - 1;
但已从1
开始rand:)