1 php 合并一个二维数组相同项,数量则相加 2 3 $arr = array( 4 array( 5 ‘user_id‘ => 100, 6 ‘goods_id‘ => 10, 7 ‘number‘ => 1, 8 ), 9 array( 10 ‘user_id‘ => 100, 11 ‘goods_id‘ => 10, 12 ‘number‘ => 2, 13 ), 14 array( 15 ‘user_id‘ => 100, 16 ‘goods_id‘ => 10, 17 ‘number‘ => 2, 18 ), 19 array( 20 ‘user_id‘ => 100, 21 ‘goods_id‘ => 10, 22 ‘number‘ => 2, 23 ), 24 ); 25 理想结果: 26 $arr = array( 27 array( 28 ‘user_id‘=> 100, 29 ‘goods_id‘ => 10, 30 ‘number‘ => 7, 31 ), 32 33 ); 34 35 36 foreach($arr as $k=>$v){ 37 if(!isset($item[$v[‘user_id‘]])){ 38 $item[$v[‘user_id‘]]=$v; 39 }else{ 40 $item[$v[‘user_id‘]][‘number‘]+=$v[‘number‘]; 41 } 42 } 43 print_r($item);
时间: 2024-10-14 22:12:26