E
E
ExistingNickname2017-07-25 20:14:52
PHP
ExistingNickname, 2017-07-25 20:14:52

How to add values ​​by key?

There is an array $arr, there is a loop in which I get the key and value. You need to add a key (string) and a value (int) to the array, if the key matches, the value should be added to the new value.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JaxxDexx, 2017-07-25
@ExistingNickname

if (array_key_exists($key, $arr)
    $arr[$key] = $arr[$key] + $value
else
    $arr[$key] = $value

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question