$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
谢谢
2 个解决方案
#1
44
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_uniqueDocs的函数。这里重要的是将第二个参数设置为SORT_REGULAR,然后工作很简单:
array_unique($result, SORT_REGULAR);
The meaning of the SORT_REGULAR
flag is:
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).
这就是你想要的。您想要在这里比较arraysDocs并且不要将它们的类型更改为字符串(如果未设置参数,则默认为默认值)。
array_unique
does a strict comparison (===
in PHP), for arrays this means:
array_unique进行严格的比较(在PHP中为===),对于数组,这意味着:
$a === $b
TRUE
if $a and $b have the same key/value pairs in the same order and of the same types.$ a === $ b如果$ a和$ b具有相同顺序和相同类型的相同键/值对,则为TRUE。
Output (Demo):
输出(演示):
Array
(
[0] => Array
(
[a] => 1
[b] => Hello
)
[1] => Array
(
[a] => 1
[b] => other
)
)
#2
16
First things first, you can not use plain array_unique
for 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.
首先,你不能使用plain 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 magic for 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_array
is working for arrays, where array_unique
does not.
具有讽刺意味的是,in_array适用于数组,其中array_unique不支持。
#1
44
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_uniqueDocs的函数。这里重要的是将第二个参数设置为SORT_REGULAR,然后工作很简单:
array_unique($result, SORT_REGULAR);
The meaning of the SORT_REGULAR
flag is:
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).
这就是你想要的。您想要在这里比较arraysDocs并且不要将它们的类型更改为字符串(如果未设置参数,则默认为默认值)。
array_unique
does a strict comparison (===
in PHP), for arrays this means:
array_unique进行严格的比较(在PHP中为===),对于数组,这意味着:
$a === $b
TRUE
if $a and $b have the same key/value pairs in the same order and of the same types.$ a === $ b如果$ a和$ b具有相同顺序和相同类型的相同键/值对,则为TRUE。
Output (Demo):
输出(演示):
Array
(
[0] => Array
(
[a] => 1
[b] => Hello
)
[1] => Array
(
[a] => 1
[b] => other
)
)
#2
16
First things first, you can not use plain array_unique
for 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.
首先,你不能使用plain 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 magic for 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_array
is working for arrays, where array_unique
does not.
具有讽刺意味的是,in_array适用于数组,其中array_unique不支持。