2

我有2个数组:

$big_array = array (
                    array (
                            array (
                                      'correct' => FALSE,
                                      'answer' => 'false answer1'
                            ),
                            array (
                                      'correct' => FALSE,
                                      'answer' => 'false answer2'
                            ),
                            array (
                                      'correct' => FALSE,
                                      'answer' => 'false answer3'
                            ),                  
                            array (
                                      'correct' => TRUE,
                                      'answer' => 'correct answer'
                            )
                    )
);

$small_array = array (
                    array (
                            array (
                                      'correct' => FALSE,
                                      'answer' => 'false answer1'
                            )
                    )
);

我想做,array_diff($big_array, $small_array)但它没有按照我想要的方式工作,目前它给了我一个空数组,我希望结果是:

$outcome_array= array (
                    array (
                            array (
                                      'correct' => FALSE,
                                      'answer' => 'false answer2'
                            ),
                            array (
                                      'correct' => FALSE,
                                      'answer' => 'false answer3'
                            ),                  
                            array (
                                      'correct' => TRUE,
                                      'answer' => 'correct answer'
                            )
                    )
);

有什么建议么?

4

3 回答 3

13

是的,您可以使用以下功能为您提供完美的答案

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);

输出

如你所愿

Array

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

            [2] => Array
                (
                    [correct] => 
                    [answer] => false answer3
                )

            [3] => Array
                (
                    [correct] => 1
                    [answer] => correct answer
                )

        )

)

在这里查看更多信息

于 2013-05-03T13:01:35.067 回答
0

编辑上面的代码检查是否 arr1 更大或 arr2 更大

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);
} 
于 2014-07-06T02:08:27.533 回答
0

我刚刚编辑了 Murali Kumar 的函数,因为我需要更多信息。它将检查数组是否大小相同并包含相同的值。

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);
}
于 2015-06-03T11:02:06.467 回答