Skip to content
Advertisement

Use an array of ordered date expressions to populate a new array with data from a second array related by its numeric keys

I am not great with arrays, but I have to build a new array from two arrays, the new array has to use the order of the first array but the data of the second but in the same order of the first.

The first array and order:

JavaScript

And the second array I need to use the values of the second, but the keys in this represent the month.

JavaScript

So the desired outcome would be

JavaScript

Advertisement

Answer

Use array_map() to map the numeric data to the month-year expression. To relate the data’s numeric keys to the date expressions, use a datetime class plus a convert method, or just call date(strtotime()).

Code: (Demo) (Classic loop)

JavaScript

Very relevant page: In PHP given a month string such as “November” how can I return 11 without using a 12 part switch statement?

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