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条)

报告相同问题?

悬赏问题

  • ¥30 STK matlab python仿真
  • ¥15 关于IMageEnView 图标定位问题
  • ¥20 求解答(matlab)
  • ¥30 ffmpeg库使用过程中遇到的问题
  • ¥15 pyqt5 中python如何通过Qtwebchannel主动发消息给web前端
  • ¥15 关于HTML中title获取xml内容的问题
  • ¥15 fanuc机器人PRIO083数字信号未复原错误,如何解决?
  • ¥20 如何为现有电路板增加远程控制功能
  • ¥15 UE5打包失败,求解决
  • ¥15 请问STM32G431的CANOPEN协议函数怎么写