"how to get an array of specific “key” in multidimensional array without looping" Code Answer

2

Since PHP 5.5, you can use array_column:

$ids = array_column($users, 'id');

This is the preferred option on any modern project. However, if you must support PHP<5.5, the following alternatives exist:

Since PHP 5.3, you can use array_map with an anonymous function, like this:

$ids = array_map(function ($ar) {return $ar['id'];}, $users);

Before (Technically PHP 4.0.6+), you must create an anonymous function with create_function instead:

$ids = array_map(create_function('$ar', 'return $ar["id"];'), $users);
By davidjohn.bm-043f0e2a51fe on October 18 2022

Answers related to “how to get an array of specific “key” in multidimensional array without looping”

Only authorized users can answer the search term. Please sign in first, or register a free account.