计算两个数组之间的差异

时间:2016-03-01 10:18:14

标签: php arrays

在PHP中,如何比较两个数组并在第三个数组中得到结果?

此结果必须包含旧值和新值。目标是显示一个HTML表格,其标题为" field |旧价值|新价值"因此,用户可以逐个比较所有值。

第一个阵列:

$array1 = array(
  'key1' => array(
    'key1.1' => 'value',
    'key1.2' => 'value',
  ),
  'key2' => array(
    'key2.1' => 'value',
    'key2.2' => 'value',
  ),
  'key3' => array(
    array('key3.1' => 'value'),
    array('key3.2' => 'value'),
  ),
);

第二阵列:

$array2 = array(
  'key1' => array(
    'key1.1' => 'value',
    'key1.2' => 'value',
  ),
  'key2' => array(
    'key2.1' => 'value',
    'key2.2' => 'value',
  ),
  'key3' => array(
    array('key3.1' => 'value'),
    array('key3.2' => 'value'),
  ),
);

我的期望:

$results = array(
  'key1' => array(
    'key1.1' => array(
      'old' => 'old_value',
      'new' => 'new_value',
    ),
    'key1.2' => array(
      'old' => 'old_value',
      'new' => 'new_value',
    ),
  ),
  'key2' => array(
    'key2.1' => array(
      'old' => 'old_value',
      'new' => 'new_value',
    ),
    'key2.2' => array(
      'old' => 'old_value',
      'new' => 'new_value',
    ),
  ),
  'key3' => array(
    array(
      'key3.1' => array(
        'old' => 'old_value',
        'new' => 'new_value')
    ),
    array(
      'key3.1' => array(
        'old' => 'old_value',
        'new' => 'new_value'),
    )
  ),
);

我已经尝试过但没有成功:

function array_diff_assoc_recursive($array1, $array2) {

  $exclude = array(
    'custom_key'
  );

  $difference = array();
  foreach($array1 as $key => $value) {
    if(is_array($value)){
      if( !isset($array2[$key]) || !is_array($array2[$key]) ) {
        if(!in_array($key,$exclude)){
          $difference[$key]['old'] = $value;
          $difference[$key]['new'] = $array2[$key];
        }
      } else {
        $new_diff = array_diff_assoc_recursive($value, $array2[$key]);
        if(!empty($new_diff))
          $difference[$key] = $new_diff;
      }
    } else if(!array_key_exists($key,$array2) || $array2[$key] !== $value) {
      if(!in_array($key,$exclude)){
        $difference[$key]['old'] = $value;
        $difference[$key]['new'] = $array2[$key];
      }
    }
  }
  return $difference;
}

2 个答案:

答案 0 :(得分:1)

使用array_walk_recursive准备数组,然后将它们组合起来。请注意,原始数组将被更改

array_walk_recursive($array1, function(&$i) { if(!is_array($i)) $i = array('old'=> $i); });
array_walk_recursive($array2, function(&$i) { if(!is_array($i)) $i = array('new'=> $i); });

print_r(array_merge_recursive($array1, $array2));

demo

答案 1 :(得分:1)

试试这段代码,我相信这对你有用

 <?php 
echo "<pre>";
$array1 = array(
  'key1' => array(
    'key1.1' => 'aaa',
    'key1.2' => 'xxx',
    'key1.3' => 'vvv',
  ),
  'key2' => array(
    'key2.1' => 'eee',
    'key2.2' => 'fff',
    'key2.3' => 'ggg',
  ),
) ;
echo "Array 1: </br>";
print_r($array1);

$array2 = array(
  'key1' => array(
    'key1.1' => 'aaa',
    'key1.2' => 'ddd',
    'key1.3' => 'ccc',
  ),
  'key2' => array(
    'key2.1' => 'hhh',
    'key2.2' => 'fff',
    'key2.3' => 'ttt',
  ),
); 
echo "Array 2:</br>";
print_r($array2);

$result='';
foreach($array1 as $key=> $val)
{
    foreach($val as $k=> $v)
    {
        if($v != $array2[$key][$k])
        {
            $result[$key][$k]['old']= $array2[$key][$k] ;
            $result[$key][$k]['new']= $v;
        }       
    }
}
echo "Compared Result: </br>";
echo "<pre>"; print_r($result);
?>

这将输出

Array 1: 
Array
(
    [key1] => Array
        (
            [key1.1] => aaa
            [key1.2] => xxx
            [key1.3] => vvv
        )

    [key2] => Array
        (
            [key2.1] => eee
            [key2.2] => fff
            [key2.3] => ggg
        )

)
Array 2:
Array
(
    [key1] => Array
        (
            [key1.1] => aaa
            [key1.2] => ddd
            [key1.3] => ccc
        )

    [key2] => Array
        (
            [key2.1] => hhh
            [key2.2] => fff
            [key2.3] => ttt
        )

)
Compared Result: 

Array
(
    [key1] => Array
        (
            [key1.2] => Array
                (
                    [old] => ddd
                    [new] => xxx
                )

            [key1.3] => Array
                (
                    [old] => ccc
                    [new] => vvv
                )

        )

    [key2] => Array
        (
            [key2.1] => Array
                (
                    [old] => hhh
                    [new] => eee
                )

            [key2.3] => Array
                (
                    [old] => ttt
                    [new] => ggg
                )

        )

)