Skip to content
Advertisement

Combine/Merge arrays that have the same keys PHP

I have an array that has some keys that are repeating multiple times. Would like to combine them so that I can have them in one array, within the same array. I have an array of this type;

JavaScript

I would like an output of the following kind;

JavaScript

I have tried array_merge, array_merge_recursive but all are not working.

JavaScript

Kindly assist if you have an idea on how to solve this in PHP.

Advertisement

Answer

You could do it with a few nested array_walk() calls:

JavaScript

See the demo here: https://rextester.com/RFLQ18142

It produces this result:

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