multidimensional array difference php

前端 未结 12 1178
离开以前
离开以前 2020-12-01 17:05

I have two multidimensional arrays and I want the difference. For eg. I have taken two-dimensional two arrays below

$array1 = Array (
       [a1] => Array         


        
12条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 17:26

    Please check if I understand you correctly then this code snippet can help to you solve your problem. I have tested it for your specified problem only. if there are other testcases for which you want to run this, you can tell me to adjust the code.

    $a1 = array(
        'a1' => array('a_name' => 'aaa', 'a_value' => 'aaaaa'),
        'b1' => array('b_name' => 'bbb', 'b_value' => 'bbbbbb'),
        'c1' => array('c_name' => 'ccc', 'c_value' => 'cccccc')
    );
    
    $a2 = array(
        'b1' => array('b_name' => 'zzzzz'),
    );
    
    $result = check_diff_multi($a1, $a2);
    print '
    ';
    print_r($result);
    print '
    '; 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; }

    EDIT: added tweak to code.

提交回复
热议问题