Possible Duplicate:
php multi-dimensional array remove duplicate可能的重复:php多维数组删除重复
I have an array like this:
我有一个这样的数组:
$a = array (
0 => array ( 'value' => 'America', ),
1 => array ( 'value' => 'England', ),
2 => array ( 'value' => 'Australia', ),
3 => array ( 'value' => 'America', ),
4 => array ( 'value' => 'England', ),
5 => array ( 'value' => 'Canada', ),
)
How can I remove the duplicate values so that I get this:
如何删除重复的值,从而得到以下结果:
$a = array (
0 => array ( 'value' => 'America', ),
1 => array ( 'value' => 'England', ),
2 => array ( 'value' => 'Australia', ),
4 => array ( 'value' => 'Canada', ),
)
I tried using array_unique, but that doesn't work due to this array being multidimensional, I think.
我试过使用array_unique,但是由于这个数组是多维的,所以我认为它不起作用。
Edit: I also need this array to be multi-dimensional and in this format, I can't flatten it.
编辑:我还需要这个数组是多维的,在这种格式下,我不能使它变平。
3 个解决方案
#1
85
array_unique
is using string conversion before comparing the values to find the unique values:
array_unique使用字符串转换来比较值,以找到惟一的值:
Note: Two elements are considered equal if and only if
(string) $elem1 === (string) $elem2
. In words: when the string representation is the same. The first element will be used.注意:当且仅当(string) $elem1 === (string) $elem2时,两个元素被认为是相等的。换句话说:当字符串表示形式相同时。将使用第一个元素。
But an array will always convert to Array
:
但数组总是会转换为数组:
var_dump("Array" === (string) array());
You can solve this by specifying the SORT_REGULAR mode in the second parameter of array_unique
:
可以通过在array_unique的第二个参数中指定SORT_REGULAR模式来解决这个问题:
$unique = array_unique($a, SORT_REGULAR);
Or, if that doesn’t work, by serializing the arrays before and unserializing it after calling array_unique
to find the unique values:
或者,如果不行,在调用array_unique后对数组进行序列化和反序列化,以找到惟一的值:
$unique = array_map('unserialize', array_unique(array_map('serialize', $a)));
#2
10
Here :)
在这里:)
<?php
$a = array (
0 => array ( 'value' => 'America', ),
1 => array ( 'value' => 'England', ),
2 => array ( 'value' => 'Australia', ),
3 => array ( 'value' => 'America', ),
4 => array ( 'value' => 'England', ),
5 => array ( 'value' => 'Canada', ),
);
$tmp = array ();
foreach ($a as $row)
if (!in_array($row,$tmp)) array_push($tmp,$row);
print_r ($tmp);
?>
#3
1
Use SORT_REGULAR flag.
使用SORT_REGULAR国旗。
$unique_array = array_unique($a, SORT_REGULAR);
I'm not sure why it helps but it does. At least with php 5.3
我不知道它为什么有用,但它确实有用。至少是php 5.3
#1
85
array_unique
is using string conversion before comparing the values to find the unique values:
array_unique使用字符串转换来比较值,以找到惟一的值:
Note: Two elements are considered equal if and only if
(string) $elem1 === (string) $elem2
. In words: when the string representation is the same. The first element will be used.注意:当且仅当(string) $elem1 === (string) $elem2时,两个元素被认为是相等的。换句话说:当字符串表示形式相同时。将使用第一个元素。
But an array will always convert to Array
:
但数组总是会转换为数组:
var_dump("Array" === (string) array());
You can solve this by specifying the SORT_REGULAR mode in the second parameter of array_unique
:
可以通过在array_unique的第二个参数中指定SORT_REGULAR模式来解决这个问题:
$unique = array_unique($a, SORT_REGULAR);
Or, if that doesn’t work, by serializing the arrays before and unserializing it after calling array_unique
to find the unique values:
或者,如果不行,在调用array_unique后对数组进行序列化和反序列化,以找到惟一的值:
$unique = array_map('unserialize', array_unique(array_map('serialize', $a)));
#2
10
Here :)
在这里:)
<?php
$a = array (
0 => array ( 'value' => 'America', ),
1 => array ( 'value' => 'England', ),
2 => array ( 'value' => 'Australia', ),
3 => array ( 'value' => 'America', ),
4 => array ( 'value' => 'England', ),
5 => array ( 'value' => 'Canada', ),
);
$tmp = array ();
foreach ($a as $row)
if (!in_array($row,$tmp)) array_push($tmp,$row);
print_r ($tmp);
?>
#3
1
Use SORT_REGULAR flag.
使用SORT_REGULAR国旗。
$unique_array = array_unique($a, SORT_REGULAR);
I'm not sure why it helps but it does. At least with php 5.3
我不知道它为什么有用,但它确实有用。至少是php 5.3