Sum elements of array the same key?
My function
function array_push_assoc(&$array, $key, $value){
echo $key.".".$value."<p>";
$array[$key] = $value;
return $array;
}
OUTPUT
USD.736.00
USD.100.00
Array开发者_运维百科
(
[USD] => 100.00
)
EUR.736.00
USD.100.00
Array
(
[EUR] => 736.00
[USD] => 100.00
)
I WANT OUTPUT
USD.736.00
USD.100.00
Array
(
[USD] => 836.00 // sum all the same currency
)
EUR.736.00
USD.100.00
Array
(
[EUR] => 736.00
[USD] => 100.00
)
Anybody Know how to do this?Please help .Thanks
I'm not sure, but i believe that is what you want:
function array_push_assoc(&$array, $key, $value){
echo $key.".".$value."<p>";
if (isset($array[$key]))
$array[$key] += $value;
else
$array[$key] = $value;
return $array;
}
精彩评论