如果
值重复,是否有一个 php 代码可以移动至少 5 次迭代的多维数组?
这是一个示例代码。
<?php
$results = array(
array('value' => 10),
array('value' => 2),
array('value' => 1),
array('value' => 3),
array('value' => 2), //This will move
array('value' => 4),
array('value' => 5),
array('value' => 5), //This will move
array('value' => 3), //This will move
array('value' => 4), //This will move
array('value' => 10), //Ok reach minimum of 5 count
array('value' => 9),
array('value' => 8),
array('value' => 7),
array('value' => 7), // This will move
array('value' => 8), // This will move
array('value' => 1), //Ok reach minimum of 5 count
array('value' => 6),
array('value' => 6), // This will move
array('value' => 19) //Ok reach minimum of 5 count
);
);
?>
这是基本思想。如果他们在至少 5 次迭代中发现重复项,我想移动这些值。迭代的移动可以大于 5。数据可以是随机的。它将循环以找到更好的结果。这可能吗?
这是我的预期结果。这可以是可以满足逻辑的其他结果。
<?php
$results = array(
array(
array('value' => 6), //Ok
array('value' => 9), //Ok
array('value' => 2), //Ok
array('value' => 7), //Ok
array('value' => 1), //Ok
array('value' => 4), //Ok
array('value' => 8), //Ok
array('value' => 5), //Ok
array('value' => 9), //Ok
array('value' => 2), //Ok
array('value' => 3), //Ok
array('value' => 6), //Ok
array('value' => 4), //Ok
array('value' => 10), //Ok
array('value' => 8), //Ok
array('value' => 7), //Ok
array('value' => 1), //Ok
array('value' => 3), //Ok
array('value' => 5), //Ok
array('value' => 10) //Ok
);
);
?>
希望你能帮助我。
编辑:
这是我的代码
<?php
$required_array = array();
$temp = "";
$temp2 = "";
foreach($array as $key=>$child) {
if( $child["value"]==$temp ) {
$i = $key+5;
$required_array[$i] = $child;
} else {
$i = $key;
//if(isset($required_array[$i])) $i++;
while(isset($required_array[$i])) {
$i++;
}
$required_array[$i] = $child;
}
$temp = $child["value"];
}
ksort($required_array);
print_r($required_array);
//Tried this but always move on five iterations and found duplicate within the range of five
array_chunk解决我的问题。
<?php
$chunks = array_chunk($array, ceil(count($array)/5));
$array = array();
for($x = 0, $numX = count($chunks[0]); $x < $numX; $x++){
for($y = 0, $numY = count($chunks); $y < $numY; $y++){
if(isset($chunks[$y][$x]))
//echo $x.' '.$y.'<br>';
$array[] = $chunks[$y][$x];
}
}
print_r($array);
?><br>
无论如何,我的下一个问题是如果我在一个数组中有两行。就像下面的例子。
<?php
$array = array(
array('value' => 1, 'value2' => 2),
array('value' => 3, 'value2' => 1)
);
?><br>
如何检查两端(值和值 2)是否重复并使用了我现有的代码?