Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Merge and sum stdclass arrays

$
0
0

Lets say i have two arrays:

1:Array(    [0] => stdClass Object        (            [uuid] => 12e3b273-ef84-4960-9f81-16cc35e69a58            [sum] => 10        )    [1] => stdClass Object        (            [uuid] => 345265aa-5f85-405d-b256-9f2cc706d222            [sum] => 20        )    [2] => stdClass Object        (            [uuid] => 8650b97e-f7c0-4d4c-a065-a082bd4fdbce            [sum] => 30        ))2:Array(    [0] => stdClass Object        (            [uuid] => 6be4da35-f0d0-4b24-99ed-ad06b95354ce            [sum] => 200        )    [1] => stdClass Object        (            [uuid] => 8650b97e-f7c0-4d4c-a065-a082bd4fdbce            [sum] => 100        ))

How to merge them and sum value of "sum" if it excist in first array?

Final array should look like:

Array(    [0] => stdClass Object        (            [uuid] => 12e3b273-ef84-4960-9f81-16cc35e69a58            [sum] => 10        )    [1] => stdClass Object        (            [uuid] => 345265aa-5f85-405d-b256-9f2cc706d222            [sum] => 20        )    [2] => stdClass Object        (            [uuid] => 8650b97e-f7c0-4d4c-a065-a082bd4fdbce            [sum] => 130 (100 + 30)        )    [3] => stdClass Object (added because it doesn´t excist in first array)        (            [uuid] => 6be4da35-f0d0-4b24-99ed-ad06b95354ce            [sum] => 200        ))

Have no clue where to start.


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>