phparraysrecursionmultidimensional-arraymerging-data

Recursively merge two multidimensional arrays synchronously to show values side-by-side


In PHP, how can I compare two array and get result in a third array ?

This result must contain old and new values. The goal is to display an HTML table with a header like "field | old value | new value" as this, the user can compare all values one by one.

First array :

$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'),
  ),
);

Second array :

$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'),
  ),
);

What I expect :

$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'),
    )
  ),
);

What I have already tried without success :

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

Solution

  • try this code, i'm sure this will work for you

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

    This will Output

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