PHP:检查多维数组中的重复值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4948946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:20:10  来源:igfitidea点击:

PHP: Check for duplicate values in a multidimensional array

phparraysduplicatesmultidimensional-array

提问by Maldoror

I have this issue with multidimensional arrays.

我对多维数组有这个问题。

Given the following multidimensional array:

给定以下多维数组:

Array(
[0] => Array("a", "b", "c")
[1] => Array("x", "y", "z")
[2] => Array("a", "b", "c")
[3] => Array("a", "b", "c")
[4] => Array("a", "x", "z")
)

I want to check its values and find duplicates (i.e. keys 0, 2 and 3) leaving just one key - value pair deleting the others, resulting in somthing like this:

我想检查它的值并找到重复项(即键 0、2 和 3),只留下一个键 - 值对删除其他键,结果是这样的:

Array(
    [0] => Array("a", "b", "c")
    [1] => Array("x", "y", "z")
    [2] => Array("a", "x", "z")
    )

How can I do that??

我怎样才能做到这一点??

回答by Tim Cooper

This will remove duplicate items from your array using array_unique():

这将使用array_unique()以下命令从数组中删除重复项:

$new_arr = array_unique($arr, SORT_REGULAR);

回答by JF Dion

You can simply do it using in_array()

你可以简单地使用 in_array()

$data = Array(
    0 => Array("a", "b", "c"),
    1 => Array("x", "y", "z"),
    2 => Array("a", "b", "c"),
    3 => Array("a", "b", "c"),
    4 => Array("a", "x", "z"),
);

$final = array();
foreach ($data as $array) {
    if(!in_array($array, $final)){
        $final[] = $array;
    }
}

which will get you something like

这会让你像

array(3) {
  [0] => array(3) {
    [0] => string(1) "a"
    [1] => string(1) "b"
    [2] => string(1) "c"
  }
  [1] => array(3) {
    [0] => string(1) "x"
    [1] => string(1) "y"
    [2] => string(1) "z"
  }
  [2] => array(3) {
    [0] => string(1) "a"
    [1] => string(1) "x"
    [2] => string(1) "z"
  }
}

回答by Gediminas

You can go smart with serialization for comparison of arrays.

您可以通过序列化来智能地比较数组。

var_dump(makeUnique($data));

function makeUnique(array $data)
{
    $serialized = array_map(create_function('$a', 'return serialize($a);'), $data);
    $unique = array_unique($serialized);
    return array_intersect_key($unique, $data);
}

Have fun

玩得开心

回答by Mrdexed

To check using array_unique on multidimensional arrays, you need to flatten it out like so, using implode.

要在多维数组上使用 array_unique 检查,您需要像这样使用内爆将其展平。

    $c=count($array)
    for($i=0;$i<$c;$i++)
    {
    $flattened=implode("~",$array[$i]);
    $newarray[$i]=$flattened;
     }
    if(count(array_unique($newarray)
    <count($newarray))
    {
    //returns true if $array contains duplicates
    //can also use array_unique on $newarray 
    //to remove   duplicates, then explode, 
    //to return to default state
    }

Hope this is helpful, took sometime to get it.

希望这有帮助,花了一些时间才能得到它。

回答by Artefacto

$arr = ...;
$final = array();
sort($arr);
foreach ($arr as $el) {
   if (!isset($prev) || $el !== $prev)
       $final[] = $el
    $prev = $el;
}

This is a more efficient1solution (log n + n instead of quadratic) but it relies on a total order between all the elements of the array, which you may not have (e.g. if the inner arrays have objects).

这是一个更有效的1解决方案(log n + n 而不是二次方),但它依赖于数组所有元素之间的总顺序,而您可能没有(例如,如果内部数组有对象)。

1More efficient than using in_array. Turns out array_uniqueactually uses this algorithm, so it has the same shortcomings.

1比使用更有效in_array。事实证明array_unique实际上使用了这个算法,所以它也有同样的缺点。