Skip to content
Advertisement

Merge and sum multiple multidimensional associative arrays

I’m needing a way to merge several arrays ( probably around 8 ) and sum any duplicate keys or sub-keys.

For example:

JavaScript

And the output should be something like this:

JavaScript

How could I combine this? I’ve tried using array_map().

But that seemed to fail with multidimensional arrays like this. Also tried using foreach(), but that got pretty convoluted.

Here’s my attempt:

JavaScript

Advertisement

Answer

Try this solution. You can add any count of arrays. But keep names as $arr1-$maxArraysCount

JavaScript

Here is output:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement