PHP - 多维数组差异

3

我希望你能帮助我解决这个问题,因为我在解决数组差异的功能上遇到了困难。我已经创建了一个函数来简化操作,但它并不能满足我的需求。谢谢,祝你更加强大!

<?php
   $arraySession = array(
      'sampleA' => array('1', '2', '3'),
      'sampleB' => array('1', '2', '3'),
   );

$arrayPost = array(
    'sampleA' => array('1'),
    'sampleB' => array('1','2'),
);

result should be:

array(
   'sampleA' => array('2', '3')
   'sampleB' => array('3'),
)

我的现有函数:

    public function array_diff_multidimensional($session, $post) { 
      $result = array();
      foreach($session as $sKey => $sValue){
          foreach($post as $pKey => $pValue) {
              if((string) $sKey == (string) $pKey) {
                  $result[$sKey] = array_diff($sValue, $pValue);
              } else {
                  $result[$sKey] = $sValue;
              }
          }
      }
      return $result;
    }

任何帮助都将不胜感激!愉快的编码!
3个回答

3

这不是我的函数,也没有经过我的测试,但这是php.net/array_diff中最早的评论之一(credit goes to thefrox at gmail dot com)。

<?php
function multidimensional_array_diff($a1, $a2) {
    $r = array();

    foreach ($a2 as $key => $second) {
        foreach ($a1 as $key => $first) {
          if (isset($a2[$key])) {
              foreach ($first as $first_value) {
                  foreach ($second as $second_value) {
                      if ($first_value == $second_value) {
                          $true = true;
                          break;
                      }
                  }
                  if (!isset($true)) {
                      $r[$key][] = $first_value;
                  }
                  unset($true);
              }
          } else {
              $r[$key] = $first;
          }
        }
    }
    return $r;
}
?>

看起来它只能处理一层数组。 - alex
嗨,dtbarne,使用此函数未能产生预期结果。请尝试以下代码:$arraySession = array( 'sampleA' => array('1', '2', '3'), 'sampleB' => array('1', '2', '3'), );$arrayPost = array( 'sampleA' => array('1','2','3'), 'sampleB' => array('1',), ); - jaym

1
你想要的大概是这样的东西:
public function array_diff_multidimensional($arr1, $arr2) {
    $answer = array();
    foreach($arr1 as $k1 => $v1) {
        // is the key present in the second array?
        if (!array_key_exists($k1, $arr2)) {
           $answer[$k1] = $v1; 
           continue;
        }

        // PHP makes all arrays into string "Array", so if both items
        // are arrays, recursively test them before the string check
        if (is_array($v1) && is_array($arr2[$k1])) {
            $answer[$k1] = array_diff_multidimensional($v1, $arr2[$k1]);
            continue;
        }

        // do the array_diff string check
        if ((string)$arr1[$k1] === (string)$arr2[$k1]) {
            continue;
        }

        // since both values are not arrays, and they don't match,
        // simply add the $arr1 value to match the behavior of array_diff
        // in the PHP core
        $answer[$k1] = $v1;
    }

    // done!
    return $answer;
}

我尝试使用这个代码,但没有得到预期的结果。请尝试以下代码:$arraySession = array( 'sampleA' => array('1', '2', '3'), 'sampleB' => array('1', '2', '3'), );$arrayPost = array( 'sampleA' => array('1','2','3'), 'sampleB' => array('1',), ); - jaym
哎呀,由于任何数组类型转换为字符串都是简单的“Array”(我忘记了这一点,来自我的PHP时代),因此所有数组都被视为相等。我将数组检查移动到字符串检查之前,现在它可以工作了(而且还可以处理n维数组)。 - Luke Sneeringer

1

假设所有的键都在两个数组中出现,这样就可以完成了:

$diff = array();
foreach ($session as $key => $values) {
    $diff[$key] = array_diff($values, $post[$key]);
}

或者,因为我感到无聊,而且array_map被低估了:

$diff = array_combine(
    array_keys($session),
    array_map(function ($a, $b) { return array_diff($a, $b); }, $session, $post)
);

(虽然假设数组已经有序。)


请澄清您问题中给出的数据和边缘情况。我甚至不确定您是指“二维”还是真正的“多维”。 - deceze

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接