为了账号安全,请及时绑定邮箱和手机立即绑定

请教下PHP数组求和的方法

请教下PHP数组求和的方法

PHP
慕仙森 2019-03-15 18:11:11
首先上数组 Array ( [0] => Array ( [tid] => 12 [aid] => 1 [openid] => oWWUP0dieOLSHl3CzjyHIcIsadhtVo [nickname] => 帅锅你好帅 [sales] => [team_name] => 帅 [team_slogan] => 酷 [title] => 测试活动 [new] => Array ( [0] => Array ( [id] => 1 [openid] => oWWUP0dieOLSHl3CzjyHIcIsadhtVo [nickname] => 帅锅你好帅 [tid] => 12 [cre_time] => 1512704950 [aid] => 0 [sales] => 12 ) [1] => Array ( [id] => 3 [openid] => oWWUP0UXRfKKA6imX_X-6OU7OKv0 [nickname] => landy [tid] => 12 [cre_time] => 1512704975 [aid] => 0 [sales] => 54 ) ) ) [1] => Array ( [tid] => 13 [aid] => 2 [openid] => oWWUP0dieOLSHl3CzjyHIcIsadhtVo [nickname] => 帅锅你好帅 [sales] => [team_name] => 帅 [team_slogan] => 酷 [title] => 测按时 [new] => Array ( [0] => Array ( [id] => 2 [openid] => oWWUP0dieOLSHl3CzjyHIcIsadhtVo [nickname] => 帅锅你好帅 [tid] => 13 [cre_time] => 1512704970 [aid] => 2 [sales] => 24 ) ) ) ) 我想计算 [0][new][sales]的总和 [1][new][sales]的总和 我不知道数组到底会有多大的....所以..... 我就知道我需要..new 下 tid 相同的话,sales 求和 求大家帮帮忙吧!~~我菜鸟... ?!
查看完整描述

3 回答

?
慕的地6264312

TA贡献1817条经验 获得超6个赞

求和还是 直接查询时候 通过数据库求和吧

查看完整回答
反对 回复 2019-03-18
?
拉风的咖菲猫

TA贡献1995条经验 获得超2个赞

foreach ($data as $key => $value) {
  $data[$key]['sum'] = array_sum(array_column($value['new'], 'sales'));
}

不知道是不是你要的结果,其实这种直接通过数据库查询更好计算吧

查看完整回答
反对 回复 2019-03-18
  • 3 回答
  • 0 关注
  • 361 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信