0


我有这个代码。=有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 的重复项,则查找重复项并移动该值。请帮助我。

4

1 回答 1

1

尝试对数组函数进行排序。这可以完成这项工作,并不完美,但它是一个很好的起点):

<?php
$values   =  array(
  array('field'   =>  1),
  array('field'   =>  1),
  array('field'   =>  2),
  array('field'   =>  2),
  array('field'   =>  2),
  array('field'   =>  3),
  array('field'   =>  3),
  array('field'   =>  4),
  array('field'   =>  4),
);

print_r($values);
uksort($values, create_function('$x,$y','return ($y === $x) ? -1 : 1;'));
print_r($values);
?>

我不知道你的价值观有多多样化,祝你好运。

于 2013-10-21T15:55:20.657 回答