162

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.

The Codesee
  • 3,714
  • 5
  • 38
  • 78
ifaour
  • 38,035
  • 12
  • 72
  • 79

4 Answers4

349

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);
Dharman
  • 30,962
  • 25
  • 85
  • 135
phihag
  • 278,196
  • 72
  • 453
  • 469
  • nice, now if there's a native function that is similar to `function ($ar) {return $ar['id'];}` return value of key that would be awesome! :-) – ifaour Nov 03 '11 at 12:26
  • 1
    well, that's not a "all in one" function, but Closure (Anynonymous functions) are a very powerfull concept that may be used for such processing, you'll rather like to know how to use them. – Boris Guéry Nov 03 '11 at 14:45
  • What if I want to get the key too? Suppose : $arr = [ 'ball'=>[ 'basket'=>5 ] ]; I want to get : $ball = ['ball'=>5]; – Angger Apr 22 '17 at 14:25
  • 2
    @Angger Then you have a different question than this one. Feel free to [ask it](http://stackoverflow.com/questions/ask) as long as you mention in detail what behavior you expect, for instance what should result if the input is `['ball' => ['golf' => 7, 'basket' => 5, 'soccer' => 6], 'shuttle' => ['badminton' => 1]]`. – phihag Apr 22 '17 at 14:52
  • Since PHP7.4 you can use `array_map` with an arrow function: `$ids = array_map(fn ($ar) => $ar['id'], $users);` – user3601546 Feb 11 '20 at 10:36
  • Would you mind updating the answer with PHP 7.4 version (arrow functions) and adding a warning about deprecated `create_function()`? – Dharman Mar 01 '20 at 14:08
  • @Dharman You should pick the first option that is an option. Therefore, there is no need to use arrow functions – In php 7.4 you can just use `array_columns`. I edited in a note saying as much. – phihag Mar 01 '20 at 18:06
15

PHP 5.5+

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

PHP 5.3+

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

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

Nicklas Kevin Frank
  • 6,079
  • 4
  • 38
  • 63
pyronaur
  • 3,515
  • 6
  • 35
  • 52
2

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. :)

deceze
  • 510,633
  • 85
  • 743
  • 889
-1

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

Muraguri E
  • 23
  • 1
  • 4
  • Your first example will not return an array, just the last value found for $user['name'] in the array. You need to push/append to the $carry and return the $carry as you go. – Progrock Dec 08 '19 at 21:20