开发者

Sum elements of array the same key?

开发者 https://www.devze.com 2023-02-07 01:27 出处:网络
My function function array_push_assoc(&$array, $key, $value){ echo $key.\".\".$value.\"<p>\";

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

精彩评论

暂无评论...
验证码 换一张
取 消