从两个多维数组中过滤掉相关数据集之间的差异

问题描述 投票:0回答:4

我有2个数组:

$big_array = [
    [
        ['correct' => FALSE, 'answer' => 'false answer1'],
        ['correct' => FALSE, 'answer' => 'false answer2'],
        ['correct' => FALSE, 'answer' => 'false answer3'],
        ['correct' => TRUE, 'answer' => 'correct answer'],
    ]
];

$small_array = [
    [
        ['correct' => FALSE, 'answer' => 'false answer1'],
    ]
];

我想做

array_diff($big_array, $small_array)
,但它没有按照我想要的方式工作。

目前,它给了我一个空数组作为结果,我希望结果是:

$outcome_array = [
    [
        ['correct' => FALSE, 'answer' => 'false answer2'],
        ['correct' => FALSE, 'answer' => 'false answer3'],
        ['correct' => TRUE, 'answer' => 'correct answer'],
    ]
];
php arrays multidimensional-array filter array-difference
4个回答
14
投票

是的,您可以使用下面的功能来获得完美的答案

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
                )

        )

)

查看此处了解更多信息


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

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

0
投票

您的输入数组似乎具有相关的数据集。 要同步迭代每个数据集并过滤掉差异,请在

array_udiff()
内进行
array_map()
调用。 演示

var_export(
    array_map(
        fn($bigSet, $smallSet) => array_udiff(
            $bigSet,
            $smallSet,
            fn($a, $b) => $a <=> $b
        ),
        $big_array,
        $small_array
    )
);

输出:

array (
  0 => 
  array (
    1 => 
    array (
      'correct' => false,
      'answer' => 'false answer2',
    ),
    2 => 
    array (
      'correct' => false,
      'answer' => 'false answer3',
    ),
    3 => 
    array (
      'correct' => true,
      'answer' => 'correct answer',
    ),
  ),
)

如果您想在结果数组中索引行,只需在每个过滤行上调用

array_values()
即可。

© www.soinside.com 2019 - 2024. All rights reserved.