将两个数组与唯一字段组合在一起

时间:2014-07-24 11:25:17

标签: php arrays array-merge

combaining A and B

组合数组和B数组,并希望结果显示在下面

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

需要帮助。

1 个答案:

答案 0 :(得分:1)

如果这是来自数据源,请查看是否可以在PHP之外执行此操作(例如,MySQL的加入)。

如果PHP是您唯一的答案,那么下面是一个解决方案。请注意,我更改了val1val2的值,因此可以更加区分。

您必须拥有某种分组约束,可以通过$groupByKey在下面的脚本中进行配置。从PID的常见情况来看,我认为这是主题密钥。

此外,如果您有两个以上的数组都遵循类似的架构(我已注释掉$c作为示例),您只需向array_merge添加更多参数。

我们的想法是通过使用固定密钥作为"指针来合并累积列表中的每个项目,"如果你愿意的话。

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

?>

提供:

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)