猿问

请教下PHP数组求和的方法

首先上数组

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 求和


求大家帮帮忙吧!~~我菜鸟...

?!

慕仙森
浏览 362回答 3
3回答

慕的地6264312

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

拉风的咖菲猫

foreach ($data as $key => $value) { $data[$key]['sum'] = array_sum(array_column($value['new'], 'sales')); } 不知道是不是你要的结果,其实这种直接通过数据库查询更好计算吧
随时随地看视频慕课网APP
我要回答