I have data as below :
Array
(
[1] => Array
(
[A] => Array
(
[AA] => 3
[AB] => 5
)
[B] => Array
(
[BA] => 2
)
[2] => Array
(
[C] => Array
(
[CA] => 4
)
[D] => Array
(
[DA] => 1
[DB] => 2
)
)
[3] => Array
(
[E] => Array
(
[EA] => 1
[EB] => 2
[EC] => 3
)
[F] => Array
(
[FA] => 0
[FB] => 7
[FC] => 7
)
)
I want to sum the value and this is my expectation :
Array(
[1] => 10
[2] => 7
[3] => 20
)
Here is my code that I used for summing the value :
$total[$country_id][$province_id][$city_id] = $amount;
$result = array();
foreach( $total as $key => $val ){
$total[$key] = array_sum ( $val );
}
can someone explain what is wrong with my code or explain how foreach work? because the result of my code is 0 and actually I just studied around 1 week about foreach. Thanks