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

Ravi picture Ravi · Nov 20, 2012 · Viewed 99.9k times · Source

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

Answer

C. E. picture C. E. · Nov 20, 2012
array_unique(array_merge($array1,$array2), SORT_REGULAR);

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