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

问题描述:

$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')   
);

有人知道这样做吗?

谢谢

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

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_REGULAR and then the job is easy:

array_unique($result, SORT_REGULAR);

SORT_REGULAR 标志的含义是:

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

compare items normally (don't change types)

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

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

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

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

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

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

输出(演示):

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

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