2011-05-17 73 views
3

我想问你的帮助,因为我很难解决这个问题。我创建了一个函数来简化数组比较,但它不足以满足我的需求。感谢和更多的权力!PHP - Mutidimensional array diff

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

不是我的功能,而不是由我测试过,但是这是在php.net/array_diff第一个评论(归功于在Gmail的点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; 
} 
?> 
+0

感谢dtbarne,IM要去尝试。 – 2011-05-17 05:17:49

+0

看起来它只能处理一个阵列。 – alex 2011-05-17 05:24:32

+0

hi dtbarne,使用此函数不会获得预期的结果。尝试:$ arraySession = array( 'sampleA'=> array('1','2','3'), 'sampleB'=> array('1','2','3'), ); $ arrayPost =阵列( \t 'sampleA'=>数组( '1', '2', '3'), \t 'sampleB'=>数组( '1',), ); – 2011-05-17 05:28:11

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; 
} 
+0

感谢卢克,我会试试这个。 – 2011-05-17 05:19:17

+0

我试过使用这个,并不会采购预期的结果。尝试:$ arraySession = array( 'sampleA'=> array('1','2','3'), 'sampleB'=> array('1','2','3'), ); $ arrayPost =阵列( \t 'sampleA'=>数组( '1', '2', '3'), \t 'sampleB'=>数组( '1',), ); – 2011-05-17 05:29:34

+0

Whups。由于任何对字符串的数组类型转换都只是“数组”(我忘记了PHP的日子),所有数组都被认为是相等的。我将数组检查移到了字符串检查之前,现在它可以工作(并且可以与n维数组一起启动)。 – 2011-05-17 14:55:41

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

+0

请澄清你的问题中的提示和边缘案例。我甚至不确定你的意思是“二维”还是真正的“多维”。 – deceze 2011-05-17 05:37:32