Skip to content
Advertisement

Group duplicate array keys in a multidimensional array into subarray

I have a multidimensional array called $songs, which outputs the following:

JavaScript

I would like to merge the arrays which have duplicate keys, so I can get the following:

JavaScript

How do I do this?

Bonus points for giving me the code to output the array like:

JavaScript

Advertisement

Answer

This should do it, it’s not exactly what you want but I don’t see a reason why you’d need to index the resulting array numerically, and then by artist.

JavaScript

And you can loop the $result array and build your HTML like this:

JavaScript

Which would result in a similar list that you described.

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