merge array and increase int value when key match in php? -


How do I merge two arrays, and raise values ​​when keys are complete? Both arrays may have keys that are not present in the other, and both have more or less keys than the other.

So I can not just take an array and loop trough and compare it with the second time since the "other" array can have more value, it is free from time to time.

The array merge is not working until it overwrites the old value.

I need to return all the unique keys to both the arrays, but whenever a key is matched, then I have to add only two values ​​together.

Array_a:

  ["BMW-B2"] = & gt; Int (1) ["BMW-N-115"] = & gt; Int (5) ["BMW-N-143"] = & gt; Int (3) ["BMW-N-13"] = & gt; Int (10) ["BMW-N-184"] = & gt; Int (4)  

array_b

  ["CR 220036"] = & gt; Int (3) ["BMW-N-13"] = & gt; Int (9) ["Cr 220822"] = & gt; Int (7)  

<(1) ["CR220036"] = & gt; Int (3) ["CR220822"] =

["BMW-B2"] = & gt; & Gt; Int (7) ["BMW-N-115"] = & gt; Int (5) ["BMW-N-143"] = & gt; Int (3) ["BMW-N-13"] = & gt; Int (19) // & lt; - This is the 9 ["BMW-N-184"] = & gt; Intent (4)

has an edge over. Order is not important.

How about:

  $ arr1 = array (1,2, 3,4); $ Arr2 = array (1,2,5 = & gt; 1); $ Result = array (); Forex Currency ($ arr = $ k => $ v) $ $ {$ result [$ k] = $ v; If (isset ($ arr2 [$ k])) {$ result [$ k] + = $ arr2 [$ k]; } $ Foreach ($ arr = $ k => $ v) {if (! Isset ($ result [$ k]) {$ result [$ k] = $ v;}} var_dump ($ result);  

Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -