已知數組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']])){ $t[$v['cust_no'].'_'.$v['lotno']]=$v; }else{ $t[$v['cust_no'].'_'.$v['lotno']]['part_count']+=$v['part_count']; } } print_r(array_values($t));
$v['cust_no'].'_'.$v['lotno']這句是什麼意思?
將 cust_no 和 lotno的值組成鍵判斷重複用的
將 cust_no 和 lotno的值組成鍵判斷重複用的
哦,原來可以這樣用啊。謝謝了!