Skip to content
Advertisement

Combine two arrays into a single array based on a common column value

I am trying to combine two arrays while respecting their shared value.

JavaScript

My expected output:

JavaScript

I have made a try by

JavaScript

My output is:

JavaScript

How can I combine the key value inside same array? or how can I bring the expected output?

Note: I am trying for value instead of key ref: PHP Array Merge two Arrays on same key

Advertisement

Answer

You can use array_map() for this. Try this –

JavaScript

It will generate the new array will all the values in both arrays.if the first array’s element is empty then the second array will be merged and vice-versa.

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