我有这个代码。=有php新手经验
$values = array(
array('field' => 1),
array('field' => 2),
array('field' => 2),
array('field' => 3),
array('field' => 4),
array('field' => 4),
);
print_r($values);
foreach($values as $k => $v) {
//Check for duplicate
//Move the value "field" to another part of the array which has no equal value on range of index 2
}
?>
如果在 2 个索引中找到重复数组,则移动数组的代码,直到到达没有重复的数组。
我的预期结果将是:可以是满足给定传导的不同结果。
1. Array(
[0] => Array('field' => 4),
Array('field' => 2),
Array('field' => 1),
Array('field' => 4),
Array('field' => 3),
Array('field' => 2)
);
2. Array(
[0] => Array('field' => 2),
Array('field' => 1),
Array('field' => 4),
Array('field' => 3),
Array('field' => 2),
Array('field' => 4)
);
我想使用 array_splice 和 array_unique 但不知道如何开始。也许它是一个while循环而不是foreach。如果发现索引为 2 的重复项,则查找重复项并移动该值。请帮助我。