Skip to content
Advertisement

What’s the best way to merge to arrays based on key values in each array?

I have these two arrays that I am currently using two loops to merge. I was wondering if there was a better way to merge these arrays:

JavaScript

I am trying to merge them into an array like this:

JavaScript

I am currently using this dual foreach loop to do it:

JavaScript

I have tried using array_merge and array_merge_recurisve, but it throws off the array keys from the $fields array.

Any thoughts or better approaches to try and achieve my desired outcome?

Advertisement

Answer

You don’t need nested loops. Just loop over the $fields array, and then access the corresponding entry in $args with an index.

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