doudui2229 2017-02-01 06:17
浏览 124
已采纳

如何在php中的foreach中的数组中累积金额

i have an multi dimensional array that's grouped. But i would like to sum up amounts and have totals for each group within the array itself. but i am struggling with summing up the amounts for each group in the array Here is how my array looks like

Array
        (
            ['Network 1'] => Array
                (
                    [0] => Array
                        (
                            [MSISDN] => 27729554427
                            [Network] => 'Network 1'
                            [Date] => '12-Mar-2016'
                            [Product] => 'Loan Product 1'
                            [Amount] => 1000.00
                        )

                    [1] => Array
                        (
                            [MSISDN] => 27725326345
                            [Network] => 'Network 1'
                            [Date] => '18-Mar-2016'
                            [Product] => 'Loan Product 2'
                            [Amount] => 3000.00
                        )
                )

            ['Network 2'] => Array
                (
                    [0] => Array
                        (
                            [MSISDN] => 27722342551
                            [Network] => 'Network 2'
                            [Date] => '16-Mar-2016'
                            [Product] => 'Loan Product 1'
                            [Amount] => 500.00
                        )

                    [1] => Array
                        (
                            [MSISDN] => 27729234533
                            [Network] => 'Network 2'
                            [Date] => '01-Apr-2016'
                            [Product] => 'Loan Product 1'
                            [Amount] => 100.00
                        )
                )
        )

this is what i want

Array
    (
        ['Network 1'] => Array
            (
                [0] => Array
                    (
                        [MSISDN] => 27729554427
                        [Network] => 'Network 1'
                        [Date] => '12-Mar-2016'
                        [Product] => 'Loan Product 1'
                        [Amount] => 1000.00
                    )

                [1] => Array
                    (
                        [MSISDN] => 27725326345
                        [Network] => 'Network 1'
                        [Date] => '18-Mar-2016'
                        [Product] => 'Loan Product 2'
                        [Amount] => 3000.00
                    )
            )
        ['Network 1 Aount total'] => 4000.00

        ['Network 2'] => Array
            (
                [0] => Array
                    (
                        [MSISDN] => 27722342551
                        [Network] => 'Network 2'
                        [Date] => '16-Mar-2016'
                        [Product] => 'Loan Product 1'
                        [Amount] => 500.00
                    )

                [1] => Array
                    (
                        [MSISDN] => 27729234533
                        [Network] => 'Network 2'
                        [Date] => '01-Apr-2016'
                        [Product] => 'Loan Product 1'
                        [Amount] => 100.00
                    )
            )
        ['Network 2 Aount total'] => 600.00    
    )

this is what i tried

        function _group_by($array, $key) {
        $return = array();
        foreach($array as $val) {
            $return[$val[$key]][] = $val;
            //$return[$val['Amount']] += $val; // this line is failing the one failing
        }

            echo '<pre>';
            print_r($return);
            echo '</pre>';
        exit();
        return $return;
    }
  • 写回答

5条回答 默认 最新

  • dqxboe2628 2017-02-01 06:24
    关注

    Do it like below (simplest one):-

    foreach ($array as $key=>$val){
       $amount =  array_sum(array_map(function($item) { 
            return $item['Amount']; 
        }, $val));
    
        $array[$key.' Amount total'] = $amount;
    }
    

    Output:-https://eval.in/728162

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效