multidimensional array difference php

前端 未结 12 1147
离开以前
离开以前 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条回答
  • One small tweak to @Zaheer Abbass solution, I got the result I wanted. Thank you very much Zaheer. Here is the final code that i used.

    function check_diff_multi($array1, $array2){
        $result = array();
        foreach($array1 as $key => $val) {
            if(isset($array2[$key])){
               if(is_array($val)  && is_array($array2[$key])){
                    $result[$key] = check_diff_multi($val, $array2[$key]);
                }
            } else {
                $result[$key] = $val;
            }
        }
    
        return $result;
    } 
    
    0 讨论(0)
  • 2020-12-01 17:20
    $out = array_diff_assoc_recursive($array1, $array2);
    

    The solution requires recursing values of array which may themselves be arrays.

    function array_diff_assoc_recursive($array1, $array2)
    {
        foreach($array1 as $key => $value)
        {
            if(is_array($value))
            {
                if(!isset($array2[$key]))
                {
                    $difference[$key] = $value;
                }
                elseif(!is_array($array2[$key]))
                {
                    $difference[$key] = $value;
                }
                else
                {
                    $new_diff = array_diff_assoc_recursive($value, $array2[$key]);
                    if($new_diff != FALSE)
                    {
                        $difference[$key] = $new_diff;
                    }
                }
            }
            elseif(!isset($array2[$key]) || $array2[$key] != $value)
            {
                $difference[$key] = $value;
            }
        }
        return !isset($difference) ? 0 : $difference;
    }
    
    0 讨论(0)
  • 2020-12-01 17:22

    print_r(array_diff_key($array1,$array2));

    0 讨论(0)
  • 2020-12-01 17:23

    So if you have arrays with empty values or with empty arrays.

    private function check_diff_multi($array1, $array2){
        $result = array();
        foreach($array1 as $key => $val) {
            if(array_key_exists($key,$array2)){
                if(is_array($val) && is_array($array2[$key]) && !empty($val)){
                    $result[$key] = $this->check_diff_multi($val, $array2[$key]);
                }
            } else {
                $result[$key] = $val;
            }
        }
        return $result;
    }
    
    0 讨论(0)
  • 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 '<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;
    }
    

    EDIT: added tweak to code.

    0 讨论(0)
  • 2020-12-01 17:26

    There are a lot of cases, where original answers will not work properly, so I wrote a better solution. One of the problems was, that if you deleted a property in array 2, the other functions didn't recognized it.

    function check_diff_multi($array1, $array2){
        $result = array();
    
        foreach($array1 as $key => $val) {
            if(is_array($val) && isset($array2[$key])) {
                $tmp = check_diff_multi($val, $array2[$key]);
                if($tmp) {
                    $result[$key] = $tmp;
                }
            }
            elseif(!isset($array2[$key])) {
                $result[$key] = null;
            }
            elseif($val !== $array2[$key]) {
                $result[$key] = $array2[$key];
            }
    
            if(isset($array2[$key])) {
                unset($array2[$key]);
            }
        }
    
        $result = array_merge($result, $array2);
    
        return $result;
    }
    

    I have also added test cases to check result:

    • Here you can find result for my function: http://sandbox.onlinephpfunctions.com/code/3bf7b032f772f8a4d184ca4151de9c3e9e9a58bc
    • Here you can find result for the accepted answer: http://sandbox.onlinephpfunctions.com/code/88efa3feb5f28bf256b5a220bd881d4a4a04130f

    As you can see, my fuctions delivers better results.

    0 讨论(0)
提交回复
热议问题