Skip to content
Advertisement

How do I use array_map recursively in PHP?

I am attempting to convert an associative array to a 2D array to allow me to export it to Google Sheets. I’ve figured out a simplistic solution that works as follows:

JavaScript

This produces the following:

JavaScript

This is the output I’m looking for, but there are 27 “rowSet”s, and it seems there must be a recursive way of performing this task.

I’ve looked at a number of custom array_map_recursive style functions but haven’t had any success. Apologies and thanks in advance, I am a terrible novice coder!

Advertisement

Answer

You can use argument unpacking.

With the ... operator, you can use all the elements under $data["resultSets"][0]["rowSet"] as additional arguments to array_map.

JavaScript

(This isn’t recursion, but I think it does what you’re trying to do.)

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