日期:2014-05-17  浏览次数:20410 次

2个数组相加,将一个数组写入另一个数组
已知数组a和数组b:

//数组a:
array (
  0 => 
  array (
    'cust_no' => '310F6 1VA5A',
    'lotno' => '2X15',
    'part_count' => '0',
  ),
  1 => 
  array (
    'cust_no' => '310F6 1VA5A',
    'lotno' => '2Z25',
    'part_count' => '5',
  ),
)

//数组b:

array (
  0 => 
  array (
    'cust_no' => '310F6 1VA5A',
    'lotno' => '2Z15',
    'part_count' => '100',
  ),
  1 => 
  array (
    'cust_no' => '310F6 1VA5A',
    'lotno' => '3115',
    'part_count' => '28',
  ),
)


把数组b的结果加入数组a,同cust_no,lotno对应的part_count累加。变成这样的结果:

array (
  0 => 
  array (
    'cust_no' => '310F6 1VA5A',
    'lotno' => '2X15',
    'part_count' => '0',
  ),
  1 => 
  array (
    'cust_no' => '310F6 1VA5A',
    'lotno' => '2Z15',
    'part_count' => '105',
  ),
  2 => 
  array (
    'cust_no' => '310F6 1VA5A',
    'lotno' => '3115',
    'part_count' => '28',
  ),
)

这样如何做到?需要用哪些数组函数?谢谢!

------解决方案--------------------
$t=array();
foreach(array_merge($a,$b) as $v){
     if(!isset($t[$v['cust_no'].'_'.$v['lotno']])){