How to combine two-dimensional arrays into an array based on specified conditions

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.