2

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

Yasen Zhelev
  • 4,045
  • 3
  • 31
  • 56
Sppidy
  • 403
  • 2
  • 5
  • 15
  • Are the name values unique in both arrays? – Mike Miller Jun 16 '15 at 11:55
  • all the id are unique not the names. i have to select all the names that does not match – Sppidy Jun 16 '15 at 12:04
  • The ID's in your example are not the same. If you have something that is unique and consistent within both record sets then just loop both arrays and replace the index with the consistent identifier and then use `array_diff_key()` – Mike Miller Jun 16 '15 at 12:18

2 Answers2

4

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');
yuk
  • 945
  • 7
  • 14
-1
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 ) ) 
khaliq
  • 98
  • 9