Search code examples
phparraysarray-merge

Combining two arrays with unique field


combaining A and B

Combining A array and B array and want the result show below

EDITED

 a = { 
      [0]=> array(2) { 
           ["pid"]=> string(1) "1" 
           ["val1"]=> string(1) "1" 
      } 
      [1]=> array(2) { 
           ["pid"]=> string(2) "12" 
           ["val1"]=> string(1) "1" 
      } 
      [2]=> array(2) { 
           ["pid"]=> string(2) "13" 
           ["val1"]=> string(2) "79" 
      }
 }

 b = { 
      [0]=> array(2) { 
           ["pid"]=> string(1) "1"
           ["val2"]=> string(1) "1" 
      } 
      [1]=> array(2) { 
           ["pid"]=> string(2) "12" 
           ["val2"]=> string(1) "1" 
      } 
      [2]=> array(2) { 
           ["pid"]=> string(2) "13" 
           ["val2"]=> string(2) "79" 
      } 
      [3]=> array(2) { 
           ["pid"]=> string(2) "61" 
           ["val2"]=> string(1) "1" 
      } 
      [4]=> array(2) { 
           ["pid"]=> string(2) "62" 
           ["val2"]=> string(2) "24" 
      }
 }

Need help.


Solution

  • If this is coming from a data source, see if there's a way you can do this outside of PHP (e.g. MySQL's JOIN).

    If PHP is your only answer, then below is a solution. Note that I changed the values of val1 and val2 so it was a bit more distinguishable.

    You must have some sort of grouping constraint, which is configurable in the script below via $groupByKey. Judging by the common occurrence of PID, I assumed this as the subject key.

    Also, if you have more than two arrays all following a similar schema (I have commented out $c as an example), you simply add more arguments to array_merge.

    The idea is to keep merging each item in the cumulative list by using a fixed key as a "pointer," if you will.

    <?php
    
    $a = array(
        array('pid' => 1, 'val1' => 'alpha'),
        array('pid' => 3, 'val1' => 'bravo'),
        array('pid' => 4, 'val1' => 'charlie')
    );
    
    $b = array(
        array('pid' => 3, 'val2' => 'delta'),
        array('pid' => 5, 'val2' => 'echo'),
        array('pid' => 1, 'val2' => 'foxtrot'),
        array('pid' => 8, 'val2' => 'golf')
    );
    
    /*
    $c = array(
        array('pid' => 3, 'val3' => 'hotel'),
        array('pid' => 5, 'val1' => 'india'),
        array('pid' => 1, 'val3' => 'juliette'),
        array('pid' => 8, 'val3' => 'kilo')
    );
    */
    
    $groupByKey = 'pid'; // this becomes the fixed key
    $merged = array_merge($a,$b); // array_merge($a,$b,$c); // cumulative container of all items in every subject array
    
    $result = array(); // the result will be stored here, e.g. a temporary "table"
    foreach ( $merged as $item ) { // $merged is essentially a table of subjects and $item is each row
        if ( !isset($result[$item[$groupByKey]]) ) { // if we haven't come across this key yet
            $result[$item[$groupByKey]] = array(); // initialize it
        }
        $result[$item[$groupByKey]] = array_merge($result[$item[$groupByKey]],$item); // consolidate all the cells for this row, later duplicate keys will cause values to be replaced
    }
    $result = array_values($result); // normalize the result keys, for the view they should increment rather than represent the group-by subjects
    
    var_dump($result); // let's see how we did
    
    ?>
    

    Provides:

    array (size=5)
      0 => 
        array (size=3)
          'pid' => int 1
          'val1' => string 'alpha' (length=5)
          'val2' => string 'foxtrot' (length=7)
      1 => 
        array (size=3)
          'pid' => int 3
          'val1' => string 'bravo' (length=5)
          'val2' => string 'delta' (length=5)
      2 => 
        array (size=2)
          'pid' => int 4
          'val1' => string 'charlie' (length=7)
      3 => 
        array (size=2)
          'pid' => int 5
          'val2' => string 'echo' (length=4)
      4 => 
        array (size=2)
          'pid' => int 8
          'val2' => string 'golf' (length=4)