简体   繁体   English

PHP - 将两个数组合并为一个数组(也删除重复项)

[英]PHP - Merging two arrays into one array (also Remove Duplicates)

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_merge for merging both arrays into one array.我正在使用array_merge将两个数组合并为一个数组。 it is giving output like this它正在给出这样的输出

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!!!!!!!我想删除这些重复的条目,或者我可以在合并之前删除它们......请帮助......谢谢!!!!!!!

array_unique(array_merge($array1,$array2), SORT_REGULAR);

http://se2.php.net/manual/en/function.array-unique.php

As already mentioned, array_unique() could be used, but only when dealing with simple data.如前所述,可以使用array_unique() ,但只能在处理简单数据时使用。 The objects are not so simple to handle.对象不是那么容易处理。

When php tries to merge the arrays, it tries to compare the values of the array members.当 php 尝试合并数组时,它会尝试比较数组成员的值。 If a member is an object, it cannot get its value and uses the spl hash instead.如果成员是对象,则无法获取其值并使用 spl 散列代替。 Read more about spl_object_hash here.在此处阅读有关 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()

It will merger two array and remove duplicate它将合并两个数组并删除重复项

<?php
 $first = 'your first array';
 $second = 'your second array';
 $result = array_merge($first,$second);
 print_r($result);
 $result1= array_unique($result);
 print_r($result1);
 ?>

Try this link link1试试这个链接link1

try to use the array_unique()尝试使用array_unique()

this elminates duplicated data inside the list of your arrays..这会消除数组列表中的重复数据。

You can use this code to get the desired result.您可以使用此代码来获得所需的结果。 It will remove duplicates.它将删除重复项。

$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");

$result=array_unique(array_merge($a1,$a2));

print_r($result);

The best solution above faces a problem when using the same associative keys, array_merge() will merge array elements together when they have the same NON-NUMBER key, so it is not suitable for the following case上面的最佳解决方案在使用相同的关联键时遇到了一个问题,当数组元素具有相同的非数字键时,array_merge() 会将它们合并在一起,因此不适用于以下情况

$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("c"=>"red","d"=>"black","e"=>"green");

If you are able output your value to the keys of your arrays instead (eg ->pluck('name', 'id')->toArray() in Eloquent), you can use the following merge method instead如果您能够将值输出到数组的键(例如,Eloquent 中的 ->pluck('name', 'id')->toArray()),则可以使用以下合并方法

array_keys(array_merge($a1, $a2))

Basically what the code does is it utilized the behavior of array_merge() to get rid of duplicated keys and return you a new array with keys as array elements, hope it helps基本上代码所做的是利用 array_merge() 的行为来摆脱重复的键并返回一个以键作为数组元素的新数组,希望它有所帮助

In my case I had to use ->toArray()就我而言,我不得不使用->toArray()

array_unique(array_merge($array1->toArray(),$array2->toArray()), SORT_REGULAR);

which results of combining both of these answers结合这两个答案的结果

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM