Multidimensional array diff

前端 未结 3 447
太阳男子
太阳男子 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);
    } 
    
    0 讨论(0)
  • 2021-01-22 06:31

    yes you can use below function to get you perfect answer

    function arrayRecursiveDiff($aArray1, $aArray2) {
      $aReturn = array();
    
      foreach ($aArray1 as $mKey => $mValue) {
        if (array_key_exists($mKey, $aArray2)) {
          if (is_array($mValue)) {
            $aRecursiveDiff = arrayRecursiveDiff($mValue, $aArray2[$mKey]);
            if (count($aRecursiveDiff)) { $aReturn[$mKey] = $aRecursiveDiff; }
          } else {
            if ($mValue != $aArray2[$mKey]) {
              $aReturn[$mKey] = $mValue;
            }
          }
        } else {
          $aReturn[$mKey] = $mValue;
        }
      }
      return $aReturn;
    } 
    
      $arr1 =    arrayRecursiveDiff($big_array,$small_array);
    

    output

    as you need

    Array
    
    (
        [0] => Array
            (
                [1] => Array
                    (
                        [correct] => 
                        [answer] => false answer2
                    )
    
                [2] => Array
                    (
                        [correct] => 
                        [answer] => false answer3
                    )
    
                [3] => Array
                    (
                        [correct] => 1
                        [answer] => correct answer
                    )
    
            )
    
    )
    

    check here for more info

    0 讨论(0)
  • 2021-01-22 06:33

    I just edited the function from Murali Kumar cause I needed one more information. It will check if the arrays are the same size and contains the same values.

    function arrayRecursiveDiff($aArray1, $aArray2) {
        if(count($aArray1) > count($aArray2)) {
            $small_arr = $aArray2;
            $big_arr = $aArray1;
            $arr1_is_big = 1;
            $arr2_is_big = 0;
        } elseif(count($aArray1) < count($aArray2)) {
            $small_arr = $aArray1;
            $big_arr = $aArray2;
            $arr1_is_big = 0;
            $arr2_is_big = 1;
        } else {
            $small_arr = $aArray1;
            $big_arr = $aArray2;
            $arr1_is_big = 0;
            $arr2_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;
            }
        }
        if($arr1_is_big)
            $aReturn["arr1_is_big"] = $arr1_is_big;
    
        if($arr2_is_big)
            $aReturn["arr2_is_big"] = $arr2_is_big;
    
        return ($aReturn);
    }
    
    0 讨论(0)
提交回复
热议问题