Multidimensional array diff

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

I got 2 arrays:

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


        
3条回答
  •  北海茫月
    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

提交回复
热议问题