删除PHP关联数组的元素复制 [英] Remove duplicated elements of associative array in 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')
);
可以在任何知道要做这件事?
Could any know to do this?
感谢
推荐答案
不管别人在这里提供你正在寻找一个名为 <$ c函数$ C> array_unique 的文件的 。这里最重要的是第二个参数设置为 SORT_REGULAR
,然后工作很简单:
Regardless what others are offering here, you are looking for a function called array_unique
Docs. 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)
这就是你想要的。你想比较数组的文件的这里,不改变自己类型为字符串(这将是缺省的,如果参数没有设置)。
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:
$一个=== $ 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
)
)
这篇关于删除PHP关联数组的元素复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!