Using array_diff() on arrays who holds several objects

2.3k views Asked by At

I have two arrays. The first array holds these values.

[a] => stdClass Object
    (
        [name] => Chris
        [id] => AaKk4j0abEkJSSxYKKnss2LTZc9BmMDrYmm1TFxkIHR8PSU50OagCsl2pgJjVTm7MrkVBVcJgydJGViRU4HHClyWVm3arL4Y5cdWKyZQHtdltg
    )

[b] => stdClass Object
    (
        [name] => John
        [id] => AaL4_sWesWgGcHsd3eoBB3NDvpekzTQHE1J9zLUJs6zZPY7urzXzuhvA3uWuW0IOGiRJPznFsCIJFzZkm2_vIlSU93njnSwgiJbM1fmI9fUulw
    )

[c] => stdClass Object
    (
        [name] => James
        [id] => AaJ3g8G-nssIY7rhMq6pGMI5989ICREh7_MQ37Vre8oNuaBkO-HFgNUWcH2tZdyFwsWOv_kG4eVliss-FY_HmRFh4gmROJKkFCFKMjzatc_2iw
    )

The second array holds these values.

[d] => stdClass Object
    (
        [id] => 1003880559625244
        [name] => Aftab
    )

[e] => stdClass Object
    (
        [id] => 448636891977424
        [name] => John
    )

[f] => stdClass Object
    (
        [id] => 809530442478820
        [name] => James
    )

[g] => stdClass Object
    (
        [id] => 809530442478820
        [name] => Chris
    )

I'm trying to compare both arrays and get only the values that do not match.
For Example in this case Aftab is the one who is not present in array 1 and is unique.
I have tried doing this with array_diff() function in PHP but it is giving this error:

Catchable fatal error: Object of class stdClass could not be converted to string

2

There are 2 answers

4
khaliq On
try this for multidimensional array   

 $array1 = array(
        'a' => array('name' => 'Chris', 'id' => 'AaKk4j0abEkJSSxYKKnss2LTZc9BmMDrYmm1TFxkIHR8PSU50OagCsl2pgJjVTm7MrkVBVcJgydJGViRU4HHClyWVm3arL4Y5cdWKyZQHtdltg'),
        'b' => array('name' => 'John', 'id' => 'AaL4_sWesWgGcHsd3eoBB3NDvpekzTQHE1J9zLUJs6zZPY7urzXzuhvA3uWuW0IOGiRJPznFsCIJFzZkm2_vIlSU93njnSwgiJbM1fmI9fUulw'),
        'c' => array('name' => 'James', 'id' => 'AaJ3g8G-nssIY7rhMq6pGMI5989ICREh7_MQ37Vre8oNuaBkO-HFgNUWcH2tZdyFwsWOv_kG4eVliss-FY_HmRFh4gmROJKkFCFKMjzatc_2iw')
    );

    $array2 = array(
        'd' => array('name' => 'Aftab','id'=>'1003880559625244'),
        'e' => array('name' => 'John','id'=>'448636891977424'),
        'f' => array('name' => 'James','id'=>'809530442478820'),
        'g' => array('name' => 'Chris','id'=>'809530442478820'),
    );
    $result = check_diff_multi($array1, $array2);
    print '<pre>';
    print_r($result);
    print '</pre>';
    function check_diff_multi($array1, $array2){
        $result = array();
        foreach($array1 as $key => $val) {
             if(isset($array2[$key])){
               if(is_array($val) && $array2[$key]){
                   $result[$key] = check_diff_multi($val, $array2[$key]);
               }
           } else {
               $result[$key] = $val;
           }
        }

        return $result;
    }

To get your desired output

[d] => stdClass Object ( [id] => 1003880559625244 [name] => Aftab ) 

$diff = array_udiff($array2,
$array1,
function($array2,$array1){
    return strcmp($array1["name"], $array2["name"]);
    }); 
    print_r($diff);

Output :

Array ( [d] => Array ( [name] => Aftab [id] => 1003880559625244 ) ) 
0
yuk On

Use array_udiff - Computes the difference of arrays by using a callback function for data comparison. http://us.php.net/array_udiff

function compare_names($a, $b)
{
    return strcmp($a->name, $b->name);
}
$newarr = array_udiff($b, $a, 'compare_names');