php php多维数组去除重复
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1861682/
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
php multi-dimensional array remove duplicate
提问by Kevin Carmody
Not sure if this question is a duplicate in need of removal, but I couldn't find the answer elsewhere so I'll have a go at asking.
不确定这个问题是否是需要删除的重复问题,但我在其他地方找不到答案,所以我会问一下。
I've got a 2d array that looks as follows:
我有一个二维数组,如下所示:
Array
(
[0] => Array
(
[0] => dave
[1] => jones
[2] => [email protected]
)
[1] => Array
(
[0] => john
[1] => jones
[2] => [email protected]
)
[2] => Array
(
[0] => bruce
[1] => finkle
[2] => [email protected]
)
)
I'd like to remove those with duplicate emails. So in the above example I'd like to just remove either [][0] or [][2]. I'm not worried about checking against names or anything like that, I just need the sub arrays to be deduplicated based on a single value.
我想删除那些有重复电子邮件的人。所以在上面的例子中,我只想删除 [][0] 或 [][2]。我不担心检查名称或类似的东西,我只需要根据单个值对子数组进行重复数据删除。
At the moment I have something like this
目前我有这样的事情
if(is_array($array) && count($array)>0){
foreach ($array as $subarray) {
$duplicateEmail[$subarray[2]] = isset($duplicateEmail[$subarray[2]]);
unset($duplicateEmail[$subarray[2]]);
}
}
but it just ain't right. Any help appreciated.
但这是不对的。任何帮助表示赞赏。
回答by Dan Soap
A quick solution which uses the uniqueness of array indexes:
使用数组索引唯一性的快速解决方案:
$newArr = array();
foreach ($array as $val) {
$newArr[$val[2]] = $val;
}
$array = array_values($newArr);
Notice 1: As visible from above, the last match for an email address is used instead of the first. This can be changed by replacing the second line with
注意 1:从上面可以看出,使用电子邮件地址的最后一个匹配项而不是第一个匹配项。这可以通过将第二行替换为
foreach (array_reverse($array) as $val) {
Notice 2: The indexes in the resulting array are somewhat mixed up. But I guess this doesn't matter...
注意 2:结果数组中的索引有些混乱。但我想这并不重要......
回答by Dipesh Parmar
Much Simpler Solution.
更简单的解决方案。
$unique = array_map('unserialize', array_unique(array_map('serialize', $array)));
echo "<pre>";
print_r($unique);
回答by robjmills
The user comments for array_unique()have a few solutions to this. For example
array_unique()的用户评论对此有一些解决方案。例如
function multi_unique($array) { foreach ($array as $k=>$na) $new[$k] = serialize($na); $uniq = array_unique($new); foreach($uniq as $k=>$ser) $new1[$k] = unserialize($ser); return ($new1); }
function multi_unique($array) { foreach ($array as $k=>$na) $new[$k] = serialize($na); $uniq = array_unique($new); foreach($uniq as $k=>$ser) $new1[$k] = unserialize($ser); return ($new1); }
from http://uk.php.net/manual/en/function.array-unique.php#57202.
来自http://uk.php.net/manual/en/function.array-unique.php#57202。
回答by Galen
$array = array(
array('dave','jones','[email protected]'),
array('dave','jones','[email protected]'),
array('dave','jones','[email protected]'),
array('dave','jones','[email protected]'),
array('dave','jones','[email protected]')
);
$copy = $array; // create copy to delete dups from
$usedEmails = array(); // used emails
for( $i=0; $i<count($array); $i++ ) {
if ( in_array( $array[$i][2], $usedEmails ) ) {
unset($copy[$i]);
}
else {
$usedEmails[] = $array[$i][2];
}
}
print_r($copy);
回答by anghazi ghermezi
User SORT_REGULAR as second parameter.
用户 SORT_REGULAR 作为第二个参数。
$uniqueArray = array_unique($array, SORT_REGULAR);
回答by mickmackusa
You can use array_column()'s clever key-assignment feature by using nullas the second parameter. This assigns new keys to each subarray while leaving the subarray data fully intact. These temporary keys ( element [2]aka "email values") will effectively cause new subarrays with duplicate emails to overwrite previous ones. Once the duplicates are purged, just re-index the array (if necessary) with array_values().
您可以array_column()通过将null用作第二个参数来使用的巧妙的键分配功能。这会为每个子阵列分配新的键,同时保持子阵列数据完整无缺。这些临时键(元素[2]又名“电子邮件值”)将有效地导致具有重复电子邮件的新子数组覆盖以前的子数组。清除重复项后,只需使用array_values().
Code: (Demo)
代码:(演示)
$array = [
['dave', 'jones', '[email protected]'],
['john', 'jones', '[email protected]'],
['bruce', 'finkle', '[email protected]']
];
var_export(array_values(array_column($array, null, 2)));
Output:
输出:
array (
0 =>
array (
0 => 'bruce',
1 => 'finkle',
2 => '[email protected]',
),
1 =>
array (
0 => 'john',
1 => 'jones',
2 => '[email protected]',
),
)
回答by Vail
My proposition:
我的提议:
protected function arrayUnique($array, $preserveKeys = false)
{
$uniqueArray = array();
$hashes = array();
foreach ($array as $key => $value) {
if (true === is_array($value)) {
$uniqueArray[$key] = $this->arrayUnique($value, $preserveKeys);
} else {
$hash = md5($value);
if (false === isset($hashes[$hash])) {
if ($preserveKeys) {
$uniqueArray[$key] = $value;
} else {
$uniqueArray[] = $value;
}
$hashes[$hash] = $hash;
}
}
}
return $uniqueArray;
}

