Php 如果对应的值至少为5次迭代且重复,则移动数组
如果值重复,是否有php代码可以移动多维数组至少5次迭代 这是一个示例代码Php 如果对应的值至少为5次迭代且重复,则移动数组,php,arrays,loops,Php,Arrays,Loops,如果值重复,是否有php代码可以移动多维数组至少5次迭代 这是一个示例代码 <?php $results = array( array('value' => 10), array('value' => 2), array('value' => 1), array('value' => 3),
<?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
);
);
?>
数组块解决我的问题
<?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)是否存在重复项并使用现有代码?到目前为止您是否尝试过任何操作?请查看我的更新问题。我不完全确定您所说的“移动”是什么意思。您能提供更清晰的输入和输出吗?将值移到另一行示例值1如果发现值在1到5之间重复,它将移到另一行您不能拥有一个所有元素都具有相同键的数组。
<?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>