概述
Array
(
[merchant] => Array
(
[0] => Array
(
[amount] => 10
[user_name] => merchant
)
[1] => Array
(
[amount] => 170
[user_name] => merchant
)
[2] => Array
(
[amount] => 25
[user_name] => merchant
)
[3] => Array
(
[amount] => 100
[user_name] => merchant
)
)
[merchant2] => Array
(
[0] => Array
(
[amount] => 5000
[user_name] => merchant2
)
[1] => Array
(
[amount] => 10
[user_name] => merchant2
)
[2] => Array
(
[amount] => 12
[user_name] => merchant2
)
[3] => Array
(
[amount] => 20
[user_name] => merchant2
)
[4] => Array
(
[amount] => 25
[user_name] => merchant2
)
[5] => Array
(
[amount] => 20
[user_name] => merchant2
)
[6] => Array
(
[amount] => 50
[user_name] => merchant2
)
[7] => Array
(
[amount] => 100
[user_name] => merchant2
)
[8] => Array
(
[amount] => 200
[user_name] => merchant2
)
[9] => Array
(
[amount] => 350
[user_name] => merchant2
)
[10] => Array
(
[amount] => 120
[user_name] => merchant2
)
[11] => Array
(
[amount] => 1000
[user_name] => merchant2
)
[12] => Array
(
[amount] => 2000
[user_name] => merchant2
)
[13] => Array
(
[amount] => 5000
[user_name] => merchant2
)
[14] => Array
(
[amount] => 50000
[user_name] => merchant2
)
[15] => Array
(
[amount] => 1000000
[user_name] => merchant2
)
)
[merchant3] => Array
(
[0] => Array
(
[amount] => 5000
[user_name] => merchant3
)
[1] => Array
(
[amount] => 20
[user_name] => merchant3
)
[2] => Array
(
[amount] => 10
[user_name] => merchant3
)
)
[merchant_test] => Array
(
[0] => Array
(
[amount] => 100000
[user_name] => merchant_test
)
)
)
if(is_array($list) && !empty($list)){
foreach ($list as $k => $amount){
$sum_amount = 0;
foreach ($amount as $item){
if($k == $item['user_name']){
$sum_amount += $item['amount'];
}
}
$newList[] = [
'user_name'=>$k,
'sum_amount'=> $sum_amount
];
}
}
$newList如下:
Array
(
[0] => Array
(
[user_name] => merchant
[sum_amount] => 305
)
[1] => Array
(
[user_name] => merchant2
[sum_amount] => 1063907
)
[2] => Array
(
[user_name] => merchant3
[sum_amount] => 5030
)
[3] => Array
(
[user_name] => merchant_test
[sum_amount] => 100000
)
)
最后
以上就是怕孤独康乃馨为你收集整理的php怎么实现数组中值的累加,php中二维数组某个键值累加的全部内容,希望文章能够帮你解决php怎么实现数组中值的累加,php中二维数组某个键值累加所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复