私人领地

php二维数组相同id的数量相加

数组样式

$arr = array(
    array(
       'user_id' => 100,
       'goods_id' => 10,
       'number' => 1,
    ),
    array(
       'user_id' => 100,
       'goods_id' => 10,
       'number' => 2,
    ),
    array(
       'user_id' => 100,
       'goods_id' => 10,
       'number' => 2,
    ),
    array(
       'user_id' => 100,
       'goods_id' => 10,
       'number' => 2,
    ),
);

处理办法

$item=array();
foreach($arr as $k=>$v){
    if(!isset($item[$v['user_id']])){
        $item[$v['user_id']]=$v;
    }else{
        $item[$v['user_id']]['number']+=$v['number'];
    }
}
print_r($item);

 

posted @ 2016-07-18 18:23  狂奔的蜗牛Snails  阅读(3114)  评论(0编辑  收藏  举报