Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

Here is my Arrays 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
)

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 this duplicate entry or can I remove it before merging... Pleas help.. Thanks!!!!!!!

share|improve this question
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 – Bgi Nov 20 '12 at 9:11
array is dynamic .. so it will not always $array1[0] and $array2[0] – Ravi Nov 20 '12 at 9:12
is there anything with which I can compare ID of each object inside an array??? – Ravi Nov 20 '12 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 – Bgi Nov 20 '12 at 9:16

4 Answers

up vote 5 down vote accepted
array_unique(array_merge($array1,$array2), SORT_REGULAR);

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

share|improve this answer
   
It will work without SORT_REGULAR – Hemantxp Nov 20 '12 at 9:19
Thanks it worked !!! thanks to all – Ravi Nov 20 '12 at 9:19
@ Hemantxp > with out SORT_REGULAR it is giving this error: Catchable fatal error: Object of class stdClass could not be converted to string – Ravi Nov 20 '12 at 9:24

try to use the array_unique()

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

share|improve this answer

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

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.