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;
}