PHP 合并数组,只有 NOT DUPLICATED 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10572546/
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 merge arrays with only NOT DUPLICATED values
提问by itsme
I need to merge two arrays into 1 array but what I need is to remove before the main data they b oth have in common (duplicated values i mean), I need only unique values when merged.
我需要将两个数组合并为 1 个数组,但我需要的是在它们共同拥有的主要数据之前删除(我的意思是重复值),合并时我只需要唯一的值。
How can i do that?
我怎样才能做到这一点?
This is the array example:
这是数组示例:
First array
第一个数组
array(3) {
[0]=> object(stdClass)#17 (1) {
["email"]=> string(7) "gffggfg"
}
[1]=> object(stdClass)#18 (1) {
["email"]=> string(6) "[email protected]"
}
[2]=> object(stdClass)#19 (1) {
["email"]=> string(6) "wefewf"
}
}
Second array
第二个数组
array(3) {
[0]=> object(stdClass)#17 (1) {
["email"]=> string(7) "[email protected]"
}
[1]=> object(stdClass)#18 (1) {
["email"]=> string(6) "wefwef"
}
[2]=> object(stdClass)#19 (1) {
["email"]=> string(6) "wefewf"
}
}
回答by Jeroen
You can combine the array_merge()function with the array_unique()function (both titles are pretty self-explanatory)
您可以将array_merge()函数与array_unique()函数结合起来(这两个标题都一目了然)
$array = array_unique (array_merge ($array1, $array2));
回答by Luca Rainone
If I understand the question correctly:
如果我正确理解问题:
$a1 = Array(1,2,3,4);
$a2 = Array(4,5,6,7);
$array = array_diff(array_merge($a1,$a2),array_intersect($a1,$a2));
print_r($array);
return
返回
Array
(
[0] => 1
[1] => 2
[2] => 3
[5] => 5
[6] => 6
[7] => 7
)
回答by kz_sergey
Faster solution:
更快的解决方案:
function concatArrays($arrays){
$buf = [];
foreach($arrays as $arr){
foreach($arr as $v){
$buf[$v] = true;
}
}
return array_keys($buf);
}
$array = concatArrays([$array1, $array2]);
回答by Marek N
A little bit late answer, but I just found that the array union operator +does the job quite greatly (found hereat the 3rd section).
有点晚了答案,但我只是发现阵列联合运营商+做这项工作存在较大(发现这里在第3节)。
$array1 + $array2 = $array //if duplicate found, the value in $array1 will be considered ($array2 value for array_merge, if keys clearly specified)
回答by Joeri Noort
I've been running some benchmarks of all the ways I can imagine of doing this. I ran tests on stacking lots of arrays of 10-20 string elements, resulting in one array with all unique strings in there. This sould be about the same for stacking just 2 arrays.
我一直在运行一些我能想象到的所有方式的基准测试。我对堆叠大量 10-20 个字符串元素的数组进行了测试,结果是一个包含所有唯一字符串的数组。对于仅堆叠 2 个数组,这应该大致相同。
The fastest I've found was the simplest thing I tried.
我发现的最快的是我尝试过的最简单的事情。
$uniques = [];
foreach($manyArrays as $arr ) {
$uniques = array_unique(array_merge($uniques, $arr));
}
I had branded this 'too naive to work', since it has to sort the uniques array every iteration. However this faster than any other method. Testing with 500.000 elements in manyArrays, each containing 10-20 strings om PHP 7.3.
我将其称为“太天真而无法工作”,因为它必须在每次迭代时对 uniques 数组进行排序。然而,这比任何其他方法都快。使用 manyArrays 中的 500.000 个元素进行测试,每个元素包含 10-20 个来自 PHP 7.3 的字符串。
A close second is this method, which is about 10% slower.
紧随其后的是这种方法,它慢了大约 10%。
$uniques = [];
foreach($manyArrays as $arr ) {
foreach($arr as $v) {
if( !in_array($v, $uniques, false) ) {
$uniques[] = $v;
}
}
}
The second method could be better in some cases, as this supports the 'strict' parameter of in_array() for strict type checking. Tho if set to true, the second option does become significantly slower than the first (around 40%). The first option does not support strict type checking.
在某些情况下,第二种方法可能更好,因为它支持in_array()的 'strict' 参数进行严格的类型检查。如果设置为 true,则第二个选项确实比第一个选项慢得多(大约 40%)。第一个选项不支持严格的类型检查。

