139

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

4
  • Because you wanna merge $array1[0] and $array2[0] not $array1 and $array2. Try to run array_merge on the first item of each array Commented Nov 20, 2012 at 9:11
  • array is dynamic .. so it will not always $array1[0] and $array2[0] Commented Nov 20, 2012 at 9:12
  • is there anything with which I can compare ID of each object inside an array??? Commented Nov 20, 2012 at 9:13
  • 1
    Forget my first comment that won't work because what you're tryin to merge are not arrays but objects. You've to do it manually Commented Nov 20, 2012 at 9:16

9 Answers 9

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

https://www.php.net/manual/en/function.array-unique.php

Sign up to request clarification or add additional context in comments.

4 Comments

@ Hemantxp > with out SORT_REGULAR it is giving this error: Catchable fatal error: Object of class stdClass could not be converted to string
Its worth noting that array_unique returns the filtered array (rather than acting directly on the array parameter passed in) so you need to store the result before you can use it
By default array_unique tries to use the array values as strings. Hence the error @Ravi ran into. If your array is only strings then you don't need the third argument. If it is not a string, or the contents cannot be implicitly cast to a string, you will need the SORT_REGULAR argument.
@Hemantwagh07 For array objects, if we do not user SORT_REGULAR it gives Recoverable fatal error: Object of class stdClass could not be converted to string in...<path to file>
9

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

Comments

8

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

Comments

4

try to use the array_unique()

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

Comments

3

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

1 Comment

This answer has oversimplified the asker's data. This task is not about merging flat arrays. This is about merging arrays of objects. Please use the asker's data when providing a solution/demonstration.
1

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

1 Comment

Whilst this will find ONLY unique entries (i.e. anything in both arrays will be removed), the OP question wanted to remove duplicates AFTER merge. So using your example, the desired result is red/green/blue/yellow kept from red/green/blue/yellow/red/green/blue after the two arrays are combined.
1

In my case I had to use ->toArray()

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

which results of combining both of these answers

Comments

0

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

1 Comment

This answer has oversimplified the asker's data. This task is not about merging flat arrays. This is about merging arrays of objects. Please use the asker's data when providing a solution/demonstration.
0

In addition to the answer above, from PHP 7.4 you can make use of the spread operator:

array_unique([...$arr1, ...$arr2]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.