Merge data into an array. if the attributes are equal, the number, item ID, and items with different attributes are not merged. There are two arrays as follows. how can we combine them into an array by conditions. {Code...} combines data into an array. if the attributes are equal, the number, item ID, and items with different attributes are not merged.
There are two arrays as follows. how can we combine them into an array by conditions.
$ Item_1 = array ([0] => array ('goods _ id' => 1, 'price' => 1, 'goods _ name' => 'item 1 ', 'goods _ attr_id '=> '1', 'num' => 1,), [1] => array ('goods _ id' => 2, 'price' => 10, 'goods _ name' => 'test item 2', 'goods _ attr_id '=> '3', 'num' => 2 ,), [2] => array ('goods _ id' => 3, 'price' => 20, 'goods _ name' => 'test item 3 ', 'goods _ attr_id '=> '4', 'num' => 3 ,),); $ item_2 = array ([0] => array ('goods _ id' => 1, 'price' => 1, 'goods _ name' => 'item 1 ', 'goods _ attr_id '=> '1, 2', 'num' => 1,), [1] => array ('goods _ id' => 2, 'price' => 10, 'goods _ name' => 'test item 2', 'goods _ attr_id '=> '3', 'num' => 2 ,), [2] => array ('goods _ id' => 4, 'price' => 30, 'goods _ name' => 'test item 4 ', 'goods _ attr_id '=> '5', 'num' => 1 ,),);
Reply content:
Merge data into an array. if the attributes are equal, the number, item ID, and items with different attributes are not merged.
There are two arrays as follows. how can we combine them into an array by conditions.
$ Item_1 = array ([0] => array ('goods _ id' => 1, 'price' => 1, 'goods _ name' => 'item 1 ', 'goods _ attr_id '=> '1', 'num' => 1,), [1] => array ('goods _ id' => 2, 'price' => 10, 'goods _ name' => 'test item 2', 'goods _ attr_id '=> '3', 'num' => 2 ,), [2] => array ('goods _ id' => 3, 'price' => 20, 'goods _ name' => 'test item 3 ', 'goods _ attr_id '=> '4', 'num' => 3 ,),); $ item_2 = array ([0] => array ('goods _ id' => 1, 'price' => 1, 'goods _ name' => 'item 1 ', 'goods _ attr_id '=> '1, 2', 'num' => 1,), [1] => array ('goods _ id' => 2, 'price' => 10, 'goods _ name' => 'test item 2', 'goods _ attr_id '=> '3', 'num' => 2 ,), [2] => array ('goods _ id' => 4, 'price' => 30, 'goods _ name' => 'test item 4 ', 'goods _ attr_id '=> '5', 'num' => 1 ,),);
foreach ($item_1 as $key => $value) { $arr[$value['goods_id'].'_'.$value['goods_attr_id']]=$value;}foreach ($item_2 as $key => $value) { if(empty($arr[$value['goods_id'].'_'.$value['goods_attr_id']])){ $arr[$value['goods_id'].'_'.$value['goods_attr_id']]=$value; }else{ $arr[$value['goods_id'].'_'.$value['goods_attr_id']]['num']=$arr[$value['goods_id'].'_'.$value['goods_attr_id']]['num']+$value['num']; } } var_dump($arr);
Sort the data first, sort the data in the same order, and then count the data.
It looks like EC. first, group by goods_id, and then merge again. you can search for the php array function, there are a lot of functions in this group that you can print and read to learn their purposes.