Skip to content
Advertisement

PHP: Combine and sort two multidimensional arrays with same value

I have two multidimensional arrays in PHP, the first one being

JavaScript

And the second one is like this:

JavaScript

I’m trying to get a union of the two arrays without replacing any value. I just want to add ‘position’ to the first array and also sort by this new value in the meantime, like this:

JavaScript

How can achieve this result in the most efficient way? Thank you!

Advertisement

Answer

You can build an array mapping ids to positions, and then loop through the a first of names (or a copy), and add the positions.

JavaScript

Output:

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