Home > Backend Development > PHP Tutorial > How to merge two-dimensional arrays into one array according to specified conditions

How to merge two-dimensional arrays into one array according to specified conditions

WBOY
Release: 2016-09-09 08:28:00
Original
1113 people have browsed it

Merge the data into an array. If the attributes are equal, the quantity and product ID will be increased. Products with different attributes will not be merged.

There are two arrays as follows. Find out how to merge them into one array according to conditions.

<code>$item_1 = array(
[0]=> array(
                'goods_id'=>1,
                'price'=>1,
                'goods_name'=>'商品1',
                'goods_attr_id'=>'1',
                'num'=>1,
        ),
        [1]=> array(
        
                'goods_id'=>2,
                'price'=>10,
                'goods_name'=>'测试商品2',
                'goods_attr_id'=>'3',
                'num'=>2,
        
        ),
        [2]=> array(
        
                'goods_id'=>3,
                'price'=>20,
                'goods_name'=>'测试商品3',
                'goods_attr_id'=>'4',
                'num'=>3,
        
        ),

);

$item_2 = array(

        [0]=> array(
        
                'goods_id'=>1,
                'price'=>1,
                'goods_name'=>'商品1',
                'goods_attr_id'=>'1,2',
                'num'=>1,
        
        ),
        [1]=> array(
        
                'goods_id'=>2,
                'price'=>10,
                'goods_name'=>'测试商品2',
                'goods_attr_id'=>'3',
                'num'=>2,
        
        ),
        [2]=> array(
        
                'goods_id'=>4,
                'price'=>30,
                'goods_name'=>'测试商品4',
                'goods_attr_id'=>'5',
                'num'=>1,
        ),
);</code>
Copy after login
Copy after login

Reply content:

Merge the data into an array. If the attributes are equal, the quantity and product ID will be increased. Products with different attributes will not be merged.

There are two arrays as follows. Find out how to merge them into one array according to conditions.

<code>$item_1 = array(
[0]=> array(
                'goods_id'=>1,
                'price'=>1,
                'goods_name'=>'商品1',
                'goods_attr_id'=>'1',
                'num'=>1,
        ),
        [1]=> array(
        
                'goods_id'=>2,
                'price'=>10,
                'goods_name'=>'测试商品2',
                'goods_attr_id'=>'3',
                'num'=>2,
        
        ),
        [2]=> array(
        
                'goods_id'=>3,
                'price'=>20,
                'goods_name'=>'测试商品3',
                'goods_attr_id'=>'4',
                'num'=>3,
        
        ),

);

$item_2 = array(

        [0]=> array(
        
                'goods_id'=>1,
                'price'=>1,
                'goods_name'=>'商品1',
                'goods_attr_id'=>'1,2',
                'num'=>1,
        
        ),
        [1]=> array(
        
                'goods_id'=>2,
                'price'=>10,
                'goods_name'=>'测试商品2',
                'goods_attr_id'=>'3',
                'num'=>2,
        
        ),
        [2]=> array(
        
                'goods_id'=>4,
                'price'=>30,
                'goods_name'=>'测试商品4',
                'goods_attr_id'=>'5',
                'num'=>1,
        ),
);</code>
Copy after login
Copy after login

<code>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);
</code>
Copy after login

Sort first, put the same ones together, and then count.

It looks like EC. First group them according to goods_id, and then merge them. For specific methods, you can search php array functions. There are many. You can learn their uses by playing with each function of this group and printing it.

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template