php 删除PHP中关联数组的重复元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13857775/
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 06:10:26  来源:igfitidea点击:

Remove duplicated elements of associative array in PHP

phparraysmultidimensional-array

提问by kn3l

$result = array(
    0=>array('a'=>1,'b'=>'Hello'),
    1=>array('a'=>1,'b'=>'other'),
    2=>array('a'=>1,'b'=>'other'),
);

If it is duplicated removed it, so the result is as follows:

如果是重复的删除它,那么结果如下:

$result = array(
    0=>array('a'=>1,'b'=>'Hello'),
    1=>array('a'=>1,'b'=>'other')   
);

Could any know to do this?

有谁知道这样做吗?

Thanks

谢谢

回答by hakre

Regardless what others are offering here, you are looking for a function called array_uniqueDocs. The important thing here is to set the second parameter to SORT_REGULARand then the job is easy:

不管其他人在这里提供什么,您都在寻找一个名为array_uniqueDocs的功能。这里重要的是将第二个参数设置为SORT_REGULAR,然后工作就很简单了:

array_unique($result, SORT_REGULAR);

The meaning of the SORT_REGULARflag is:

SORT_REGULAR国旗的含义是:

compare items normally (don't change types)

正常比较项目(不要改变类型)

And that is what you want. You want to compare arraysDocshere and do not change their type to string (which would have been the default if the parameter is not set).

这就是你想要的。您想在此处比较数组Docs并且不要将它们的类型更改为字符串(如果未设置参数,这将是默认值)。

array_uniquedoes a strict comparison (===in PHP), for arrays this means:

array_unique进行严格比较(===在 PHP 中),对于数组,这意味着:

$a === $bTRUEif $a and $b have the same key/value pairs in the same order and of the same types.

$a === $bTRUE如果 $a 和 $b 具有相同顺序和相同类型的相同键/值对。

Output (Demo):

输出(演示):

Array
(
    [0] => Array
        (
            [a] => 1
            [b] => Hello
        )

    [1] => Array
        (
            [a] => 1
            [b] => other
        )
)

回答by David Müller

First things first, you can not use plain array_uniquefor this problem because array_unique internally treats the array items as strings, which is why "Cannot convert Array to String" notices will appear when using array_unique for this.

首先,您不能使用普通array_unique来解决这个问题,因为 array_unique 在内部将数组项视为字符串,这就是为什么在为此使用 array_unique 时会出现“无法将数组转换为字符串”的通知。

So try this:

所以试试这个:

$result = array(
    0=>array('a'=>1,'b'=>'Hello'),
    1=>array('a'=>1,'b'=>'other'),
    2=>array('a'=>1,'b'=>'other')
);

$unique = array_map("unserialize", array_unique(array_map("serialize", $result)));

print_r($unique);

Result:

结果:

Array
(
    [0] => Array
        (
            [a] => 1
            [b] => Hello
        )

    [1] => Array
        (
            [a] => 1
            [b] => other
        )

)

Serialization is very handy for such problems.

序列化对于此类问题非常方便。

If you feel that's too much magicfor you, check out this blog post

如果你觉得这对你来说太神奇了,看看这篇博文

function array_multi_unique($multiArray){

  $uniqueArray = array();

  foreach($multiArray as $subArray){

    if(!in_array($subArray, $uniqueArray)){
      $uniqueArray[] = $subArray;
    }
  }
  return $uniqueArray;
}

$unique = array_multi_unique($result);

print_r($unique);

Ironically, in_arrayis working for arrays, where array_uniquedoes not.

具有讽刺意味的是,in_array正在为数组工作,而在那里array_unique却没有。