码迷,mamicode.com
首页 > 编程语言 > 详细

PHP二维数组去除重复,重复值相加

时间:2015-12-08 18:29:19      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

$arr = array(
    array(‘id‘ => 122, ‘name‘ => ‘张三‘, ‘amount‘ => ‘1‘),
    array(‘id‘ => 123, ‘name‘ => ‘李四‘, ‘amount‘ => ‘1‘),
    array(‘id‘ => 124, ‘name‘ => ‘王五‘, ‘amount‘ => ‘1‘),
    array(‘id‘ => 125, ‘name‘ => ‘赵六‘, ‘amount‘ => ‘1‘),
    array(‘id‘ => 126, ‘name‘ => ‘赵六‘, ‘amount‘ => ‘2‘),
    array(‘id‘ => 126, ‘name‘ => ‘赵六‘, ‘amount‘ => ‘2‘)
);

$new = array();

foreach($arr as $row){
    if(isset($new[$row[‘name‘]])){
        $new[$row[‘name‘]][‘amount‘] += $row[‘amount‘];
    }else{
        $new[$row[‘name‘]] = $row;
    }
}

var_dump($new);

 

PHP二维数组去除重复,重复值相加

标签:

原文地址:http://www.cnblogs.com/qhorse/p/5029660.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!