Multidimensional array diff

前端 未结 3 453
太阳男子
太阳男子 2021-01-22 05:39

I got 2 arrays:

$big_array = array (
                    array (
                            array (
                                      \'correct\' => FALS         


        
3条回答
  •  臣服心动
    2021-01-22 06:17

    Edited above code check whether or the the arr1 is bigger or arr2 is bigger

    function arrayRecursiveDiff($aArray1, $aArray2) {
    
    if(sizeof($aArray1) > sizeof($aArray2))
    {
        $small_arr = $aArray2;
        $big_arr = $aArray1;
        $arr1_is_big = 1;
    }
    else
    {
        $small_arr= $aArray1;
        $big_arr = $aArray2;
        $arr1_is_big = 0;
    }
    $aReturn = array();
    
    foreach ($big_arr as $mKey => $mValue) {
        if (array_key_exists($mKey, $small_arr)) {
            if (is_array($mValue)) {
                $aRecursiveDiff = arrayRecursiveDiff($mValue, $small_arr[$mKey]);
                if (count($aRecursiveDiff)) { $aReturn[$mKey] = $aRecursiveDiff; }
                }
            else {
                if ($mValue != $small_arr[$mKey]) {
                    $aReturn[$mKey] = $mValue;
                    }
                }
        } else {
            $aReturn[$mKey] = $mValue;
        }
    }
    $aReturn["arr1_is_big"] = $arr1_is_big;
    
    return ($aReturn);
    } 
    

提交回复
热议问题