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

Murali Kumar の関数を編集したところ、もう 1 つ情報が必要でした。配列が同じサイズで、同じ値が含まれているかどうかを確認します。

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 に答える