php 基础 二维数组以某个重复值累加

$arr = array(
    array('id' => 123, '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['id']])){
        $new[$row['id']]['amount'] += $row['amount'];
    }else{
        $new[$row['id']] = $row;
    }
}

print_r($new);

 

posted @ 2018-08-21 15:48  zhw15  阅读(456)  评论(0编辑  收藏  举报