PHP - 如何比较两个数组并删除重复值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8691329/
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 - How to compare two arrays and remove duplicate values
提问by Limeni
So this is whats bothering me.
所以这就是困扰我的事情。
I have two arrays:
我有两个数组:
$array1 = array('[param1]' ,'demo' ,'[param2]' ,'some' ,'[param3]');
$array2 = array('value1' ,'demo' ,'value2' ,'some' ,'value3');
Now I want to compare these two array's, and remove all duplicate values.
At the end I want this two array-s but without 'demo' and 'some' values in them.
I want to remove all values from array-s that have the same index key and value.
Array's will always have same number of values and indexes, I only want to compare them and remove entries that have the same index key and value, from both of them.
现在我想比较这两个数组,并删除所有重复值。
最后,我想要这两个数组,但其中没有“演示”和“一些”值。
我想从 array-s 中删除所有具有相同索引键和值的值。
数组将始终具有相同数量的值和索引,我只想比较它们并从它们两个中删除具有相同索引键和值的条目。
I'm doing something like this now:
我现在正在做这样的事情:
$clean1 = array();
$clean2 = array();
foreach($array1 as $key => $value)
{
if($value !== $array2[$key])
{
$clean1[$key] = $value;
$clean2[$key] = $array2[$key];
}
}
var_export($clean1);
echo "<br />";
var_export($clean2);
And this works! But im wondering is there any other way of doing this? Maybe without using foreach loop? Is there more elegant way of doing this?
这有效!但我想知道有没有其他方法可以做到这一点?也许不使用 foreach 循环?有没有更优雅的方式来做到这一点?
回答by Mr. BeatMasta
array_unique( array_merge($arr_1, $arr_2) );
or you can do:
或者你可以这样做:
$arr_1 = array_diff($arr_1, $arr_2);
$arr_2 = array_diff($arr_2, $arr_1);
i guess...
我猜...
回答by Shawn Janas
You can use the function array_diff in PHP that will return and array containing the keys that are the same between the two arrays.
您可以在 PHP 中使用 array_diff 函数,该函数将返回包含两个数组之间相同键的数组。
$clean1 = array_diff($array1, $array2);
回答by Mizhar Raja
$clean1 = array_diff($array1, $array2);
$clean2 = array_diff($array2, $array1);
$final_output = array_merge($clean1, $clean2);
回答by GreeKatrina
Your solution is definitely the most "elegant" (meaning the easiest to read, and least amount of code), but here is another solution that uses array_diff_ukey(). It preserves the keys, and puts them in incremental order, as you requested.
您的解决方案绝对是最“优雅”的(意味着最容易阅读,代码量最少),但这里是另一个使用array_diff_ukey() 的解决方案。它保留密钥,并按照您的要求将它们按递增顺序排列。
$array1 = ['[param1]' ,'demo' ,'[param2]' ,'some' ,'[param3]'];
$array2 = ['value1' ,'demo' ,'value2' ,'some' ,'value3'];
$clean1 = $array1;
$clean2 = array_diff_ukey(
$array2,
$array1,
// Need to have "&" so the values get set.
function($a, $b) use (&$clean1, $array1, $array2) {
// Use isset() just in case keys are not identical
// or arrays are not the same length.
if (isset($array2[$b]) && $array2[$b] === $array1[$b]) {
unset($clean1[$b]);
}
return strcmp($array2[$a], $array1[$b]);
});
print_r($clean1);
print_r($clean2);
Will return this:
将返回这个:
Array
(
[0] => [param1]
[2] => [param2]
[4] => [param3]
)
Array
(
[0] => value1
[2] => value2
[4] => value3
)
Working example here.
工作示例在这里。
回答by gauravbhai daxini
For example i have two array like below:
例如,我有两个数组,如下所示:
$array1=Array([0] => parking.jpeg);
$array2=Array ([0] => parking.jpeg [1] => logo.jpeg);
first compare from array1 to array2 like below, which will return null
首先像下面这样从 array1 到 array2 进行比较,这将返回 null
$clean1 = array_diff($array1, $array2);
second compare from array2 to array1 which will return logo.jpeg
从 array2 到 array1 的第二次比较将返回 logo.jpeg
$clean2 = array_diff($array2, $array1);
now you can merge both differences in one array like below:
现在您可以将两个差异合并到一个数组中,如下所示:
$final_output = array_merge($clean1, $clean2);
Output:Array ( [0] => logo.jpeg )
输出:Array ( [0] => logo.jpeg )
回答by Dancrumb
Since $array1
and $array2
are always the same length, you could do something like this:
由于$array1
和$array2
总是相同的长度,你可以做这样的事情:
<?php
$array1 = array('[param1]' ,'demo' ,'[param2]' ,'some' ,'[param3]');
$array2 = array('value1' ,'demo' ,'value2' ,'some' ,'value3');
$map = array_combine($array1,$array2);
$map = array_filter($map ,function ($item) use (&$map) {
$keep_data = ($item != key($map));
next($map);
return $keep_data;
});
$clean1 = array_keys($map);
$clean2 = array_values($map);
var_export($clean1);
echo "<br />";
var_export($clean2);
?>
Is it better? You decide
这个会比较好吗?你决定
回答by krejcon3
what about?
关于什么?
$res = array_diff($array1, $array_intersect($array1, $array2));
$reindexed = array_combine(array_keys($res), array_values($res));
or simply way if keys are not needed
或者简单的方法,如果不需要钥匙
array_values(array_diff($array1, $array_intersect($array1, $array2)));