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

  • 323
$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);
weinxin
我的微信
这是我的微信扫一扫
开拓者博主
  • 本文由 发表于 2021年2月3日12:44:34
  • 转载请务必保留本文链接:https://www.150643.com/554.html
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: