PHP - 将两个数组合并为一个数组(也删除重复项)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13469803/
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 - Merging two arrays into one array (also Remove Duplicates)
提问by Ravi
Hi I'm Trying to merge two arrays and also want to remove duplicate values from final Array.
嗨,我正在尝试合并两个数组,并且还想从最终数组中删除重复值。
Here is my Array 1:
这是我的数组 1:
Array
(
[0] => stdClass Object
(
[ID] => 749
[post_author] => 1
[post_date] => 2012-11-20 06:26:07
[post_date_gmt] => 2012-11-20 06:26:07
)
And this is my array 2:
这是我的数组 2:
Array
(
[0] => stdClass Object
(
[ID] => 749
[post_author] => 1
[post_date] => 2012-11-20 06:26:07
[post_date_gmt] => 2012-11-20 06:26:07
)
I'm using array_mergefor merging both arrays into one array. it is giving output like this
我array_merge用于将两个数组合并为一个数组。它正在给出这样的输出
Array
(
[0] => stdClass Object
(
[ID] => 749
[post_author] => 1
[post_date] => 2012-11-20 06:26:07
[post_date_gmt] => 2012-11-20 06:26:07
[1] => stdClass Object
(
[ID] => 749
[post_author] => 1
[post_date] => 2012-11-20 06:26:07
[post_date_gmt] => 2012-11-20 06:26:07
)
I want to remove these duplicate entries or can I remove these before merging... Pleas help.. Thanks!!!!!!!
我想删除这些重复的条目,或者我可以在合并之前删除它们......请帮助......谢谢!!!!!!!
回答by C. E.
array_unique(array_merge($array1,$array2), SORT_REGULAR);
回答by Daxen
回答by Nikola Petkanski
As already mentioned, array_unique()could be used, but only when dealing with simple data. The objects are not so simple to handle.
如前所述,可以使用array_unique(),但只能在处理简单数据时使用。对象不是那么容易处理。
When php tries to merge the arrays, it tries to compare the values of the array members. If a member is an object, it cannot get its value and uses the spl hash instead. Read more about spl_object_hash here.
当 php 尝试合并数组时,它会尝试比较数组成员的值。如果成员是对象,则它无法获取其值,而是使用 spl 散列。在此处阅读有关 spl_object_hash 的更多信息。
Simply told if you have two objects, instances of the very same class and if one of them is not a reference to the other one - you will end up having two objects, no matter the value of their properties.
简单地告诉你,如果你有两个对象,同一个类的实例,如果其中一个不是对另一个的引用 - 你最终将拥有两个对象,无论它们的属性值如何。
To be sure that you don't have any duplicates within the merged array, Imho you should handle the case on your own.
为了确保您在合并的数组中没有任何重复项,恕我直言,您应该自己处理这种情况。
Also if you are going to merge multidimensional arrays, consider using array_merge_recursive()over array_merge().
此外,如果您要合并多维数组,请考虑在array_merge()上使用array_merge_recursive()。
回答by Jhonathan H.
try to use the array_unique()
尝试使用 array_unique()
this elminates duplicated data inside the list of your arrays..
这会消除数组列表中的重复数据。
回答by kantsverma
Merging two array will not remove the duplicate you can try the below example to get unique from two array
合并两个数组不会删除重复项,您可以尝试以下示例从两个数组中获取唯一值
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$result=array_diff($a1,$a2);
print_r($result);

