How to get an array of specific "key" in multidimensional array without looping

Php

Php Problem Overview


Let's assume I have the following multidimensional array (retrieved from MySQL or a service):

array(
	array(
		[id] => xxx,
		[name] => blah
	),
	array(
		[id] => yyy,
		[name] => blahblah
	),
	array(
		[id] => zzz,
		[name] => blahblahblah
	),
)

Can we get an array of ids in one "built-in" php function call? or one line of code?
I am aware of the traditional looping and getting the value but I don't need this:

foreach($users as $user) {
	$ids[] = $user['id'];
}
print_r($ids);

Maybe some array_map() and call_user_func_array() can do the magic. [1]: https://stackoverflow.com/questions/4286734/how-to-return-an-array-and-get-the-first-element-of-it-in-one-line-in-php

Php Solutions


Solution 1 - Php

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);

Solution 2 - Php

PHP 5.5+

Starting PHP5.5+ you have array_column() available to you, which makes all of the below obsolete.

PHP 5.3+

Solution by @phihag will work flawlessly in PHP starting from PHP 5.3.0, if you need support before that, you will need to copy that wp_list_pluck.

PHP < 5.3
Wordpress 3.1+

In Wordpress there is a function called wp_list_pluck If you're using Wordpress that solves your problem.

PHP < 5.3

If you're not using Wordpress, since the code is open source you can copy paste the code in your project (and rename the function to something you prefer, like array_pick). View source here

Solution 3 - Php

If id is the first key in the array, this'll do:

$ids = array_map('current', $users);

You should not necessarily rely on this though. :)

Solution 4 - Php

You can also use array_reduce() if you prefer a more functional approach

For instance:

$userNames = array_reduce($users, function ($carry, $user) {
    array_push($carry, $user['name']);
    return $carry;
}, []);

Or if you like to be fancy,

$userNames = [];
array_map(function ($user) use (&$userNames){
    $userNames[]=$user['name'];
}, $users);

This and all the methods above do loop behind the scenes though ;)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionifaourView Question on Stackoverflow
Solution 1 - PhpphihagView Answer on Stackoverflow
Solution 2 - PhppyronaurView Answer on Stackoverflow
Solution 3 - PhpdecezeView Answer on Stackoverflow
Solution 4 - PhpMuraguri EView Answer on Stackoverflow