在包含多个对象的数组上使用array_diff()

时间:2015-06-16 11:41:25

标签: php arrays object array-difference

我有两个数组。第一个数组包含这些值。

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

第二个数组包含这些值。

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

我正在尝试比较两个数组并仅获取不匹配的值 例如,在这种情况下,Aftab是数组1中不存在且唯一的。{。} 我已尝试在PHP中使用array_diff()函数执行此操作,但它给出了此错误:

  

捕获致命错误:类stdClass的对象无法转换为字符串

2 个答案:

答案 0 :(得分:3)

使用isIE7 = isIE && ((check(/msie 7/) && docMode != 8 && docMode != 9 && docMode != 10) || docMode == 7), isIE8 = isIE && ((check(/msie 8/) && docMode != 7 && docMode != 9 && docMode != 10) || docMode == 8), isIE9 = isIE && ((check(/msie 9/) && docMode != 7 && docMode != 8 && docMode != 10) || docMode == 9), isIE10 = isIE && ((check(/msie 10/) && docMode != 7 && docMode != 8 && docMode != 9) || docMode == 10), isIE11 = isIE && ((check(/trident\/7\.0/) && docMode != 7 && docMode != 8 && docMode != 9 && docMode != 10) || docMode == 11), - 通过使用回调函数进行数据比较来计算数组的差异。 http://us.php.net/array_udiff

array_udiff

答案 1 :(得分:-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;
    }

获得所需的输出

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

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

输出

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