Skip to content
Advertisement

How to map a 2D array to 1D array in PHP

I can’t figure out how to do the following array_map in php. Any help is much appreciated.

Input:

JavaScript

Desired Output:

JavaScript

What I have so far:

Here I am mapping to an array and I know it’s not what I want but I can not figure out if there is some syntax for me to return just $x[‘slug’] => $x[‘name’] without the brackets?

JavaScript

My current output (not what I want):

JavaScript

Advertisement

Answer

There is a PHP function that can do exactly what you want: array_column()

JavaScript

That will give you:

JavaScript

Here’s a demo: https://3v4l.org/LGcES

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