PHP - 多维数组diff

时间:2011-05-17 05:02:09

标签: php arrays multidimensional-array

我想请求你的帮助,因为我很难解决这个问题。我创建了一个方便数组差异的功能,但它不足以满足我的需求。谢谢,更有力量!

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

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

结果应为:

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 个答案:

答案 0 :(得分:3)

不是我的功能而且没有经过我的测试,但这是php.net/array_diff的第一个评论之一(信用卡转到gmail dot com的thefrox)

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

答案 1 :(得分: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;
}

答案 2 :(得分: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)
);

(假设有序排列良好的阵列。)