如何在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;
    }

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

I think you are doing that in a wrong way. To get the specific column in the array we have

   array_column($array, $columnname);

function. So use this to calculate the sum and display.

I've not tested but is should work, or something like this

function _group_by($array, $key) {
    $return = array();
    foreach($array as $key=>$val) {
        $return[$key][] = $val;
        $total=0;
        foreach($val as $product){
            $total=$total+$product['Amount'];
        }
        $return[$key . ' Total Amount']=$total;
    }

    echo '<pre>';
    print_r($return);
    echo '</pre>';
    exit();
    return $return;
}

Check this function,

function _group_by($array, $key)
{
    foreach ($array as $k => $subArray) {
        $temp = 0;
        foreach ($subArray as $id => $value) {
            $temp += $value['Amount'];
        }
        $array[$k . 'Aount total'] = $temp;
    }
    return $array;
}

Simple, and working.

Try this

foreach ($array as $key=>$val){
   $amount =  array_sum(array_column($val, 'Amount'));  
    $array[$key.' Aount total'] = $amount;
}