How can i merge two arrays in php without duplicates?

Hi I'm Trying to merge two arrays and also want to remove duplicate values from final Array.

Here is my Array 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:

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. 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!!!!!!!

How can i merge two arrays in php without duplicates?

imskm

7501 gold badge13 silver badges27 bronze badges

asked Nov 20, 2012 at 9:10

How can i merge two arrays in php without duplicates?

Ravikumar SharmaRavikumar Sharma

3,6084 gold badges34 silver badges56 bronze badges

4

As already mentioned, array_unique() could be used, but only when dealing with simple data. 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. If a member is an object, it cannot get its value and uses the spl hash instead. Read more about spl_object_hash here.

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().

answered Nov 20, 2012 at 9:20

Nikola PetkanskiNikola Petkanski

4,5841 gold badge32 silver badges41 bronze badges

It will merger two array and remove duplicate


Try this link link1

answered Nov 20, 2012 at 9:16

DaxenDaxen

5083 silver badges17 bronze badges

try to use the array_unique()

this elminates duplicated data inside the list of your arrays..

answered Nov 20, 2012 at 9:15

How can i merge two arrays in php without duplicates?

Jhonathan H.Jhonathan H.

2,7121 gold badge17 silver badges26 bronze badges

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

answered Sep 6, 2018 at 9:28

kantsvermakantsverma

54710 silver badges26 bronze badges

1

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

How can i merge two arrays in php without duplicates?

answered Oct 29, 2020 at 13:21

1

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

$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 (e.g ->pluck('name', 'id')->toArray() in Eloquent), you can use the following merge method instead

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

answered Jan 14, 2021 at 8:11

Phantom1412Phantom1412

1892 silver badges9 bronze badges

1

How can I merge two arrays without duplicates in PHP?

You can use the PHP array_unique() function and PHP array_merge() function together to merge two arrays into one array without duplicate values in PHP.

How do I merge two arrays without duplicates?

5 ways to Merge two Arrays and Remove Duplicates in Javascript.
Merge arrays and de-duplicate items using concat() and filter().
Merge arrays and de-duplicate items using Set and spread operator..
Merge arrays and de-duplicate items using Set and concat().
Merge arrays and de-duplicate items using while loop and concat().

How can I merge two arrays in PHP?

PHP array_merge() Function.
Merge two arrays into one array: $a1=array("red","green"); $a2=array("blue","yellow"); ... .
Merge two associative arrays into one array: $a1=array("a"=>"red","b"=>"green"); $a2=array("c"=>"blue","b"=>"yellow"); ... .
Using only one array parameter with integer keys: $a=array(3=>"red",4=>"green");.

How do I combine two arrays from another array?

In order to merge two arrays, we find its length and stored in fal and sal variable respectively. After that, we create a new integer array result which stores the sum of length of both arrays. Now, copy each elements of both arrays to the result array by using arraycopy() function.

How can I get unique values from two arrays in PHP?

The array_diff() (manual) function can be used to find the difference between two arrays: $array1 = array(10, 20, 40, 80); $array2 = array(10, 20, 100, 200); $diff = array_diff($array1, $array2); // $diff = array(40, 80, 100, 200);