-2

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.

SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87
balaji
  • 291
  • 1
  • 4
  • 13
  • 5
    So what have you actually tried? Where do the arrays come from? Is this a database query? – Mark Baker Jul 24 '14 at 11:27
  • What does the array actually look like in PHP? Is it a one or two dimensions: `$a = array('a','b','c');` vs `$a = array('val1'=>array('a','b','c'));`? – zamnuts Jul 24 '14 at 11:31
  • both "a" and "b" are array want to combine these two array's – balaji Jul 24 '14 at 11:31
  • a=array([0]=>([pid]=>'1',[val1]='sdas'),[1]=>([pid]=>'2',[val1]='asdasd'),[2]=>([pid]=>'4',[val1]='fasdfasd')...) and b=array([0]=>([pid]=>'3',[val2]='test'),[1]=>([pid]=>'5',[val1]='new')...) – balaji Jul 24 '14 at 11:34
  • 2
    Please post array in your question in proper formating – TBI Jul 24 '14 at 11:37

1 Answers1

1

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)
zamnuts
  • 9,492
  • 3
  • 39
  • 46
  • @balaji my array format matches yours except for the values and my PIDs are integers, no? The way the solution is constructed, the actual values are arbitrary. – zamnuts Jul 24 '14 at 11:59
  • a = { [0]=> array(2) { ["pid"]=> string(1) "1" ["val1"]=> string(1) "alpha"} im not saying value that indexing "[0] ......" – balaji Jul 24 '14 at 12:02
  • @balaji `a` is an object? given the numeric keys it describes an array, however the curly braces `{` hint towards an object. Regarding the array structure you replied with, I'm still unsure what you mean. Please specify more clearly. – zamnuts Jul 24 '14 at 12:05