13

Assuming I have an array of objects in PHP, something like:

Array (
    [0] => stdClass Object (
            [id] => 1
            [name] => Title One
        )    
    [1] => stdClass Object (
            [id] => 2
            [name] => Title Two
        )

    [2] => stdClass Object (
            [id] => 7
            [name] => Title Seven
        )
)

What is the best way (i.e. fastest) to get an array of the IDs? i.e. array(1,2,7) I can loop manually but I feel there must be a better method.

Just saw this in the similar questions but there's a little debate over whether the accepted answer is really the best way, plus it's from 2 years ago. I'm on PHP 5.3.

Community
  • 1
  • 1
DisgruntledGoat
  • 70,219
  • 68
  • 205
  • 290
  • 1
    Depending on your scenario, you may want to consider using an [associative array](http://php.net/manual/en/language.types.array.php) instead of an array of objects with an `id` member. – Joey Adams Jun 24 '11 at 01:50

7 Answers7

9

You can use array_map to get the IDs from each element.

function getID($a){
   return $a->id;
}
$IDs = array_map('getID', $array);

Demo: http://ideone.com/nf3ug

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
6

Since PHP 7.0 you may use the builtin function array_column for that, which takes an input array and the name of the property you want to pluck:

$ids = array_column($input, 'id');
// array(0 => 1, 1 => 2, 2 => 7)

As a third parameter, you may optionally supply an index-key as well:

$ids = array_column($input, 'name', 'id');
// array(1 => 'Title One', 2 => 'Title Two', 7 => 'Title Seven')

Please note that, although it's already available in PHP 5.5.0, support for an array of objects was first introduced in PHP 7.0.

Karim Geiger
  • 622
  • 1
  • 8
  • 17
3

The fastest way is simply looping (foreach, for, while). Using callback functions will incur unnecessary overhead.

I would look to see if there's a way to create the list via the code that is building the initial array of objects.

webbiedave
  • 48,414
  • 8
  • 88
  • 101
2

I'm using RedBean and for some reason passing in "getID" didn't work for me, so here is how I done it:

$ids = array_map(function($val){return $val->id;}, $objects);
malhal
  • 26,330
  • 7
  • 115
  • 133
2

You can do it easily with ouzo goodies

$result = array_map(Functions::extract()->id, $objects);

or with Arrays (from ouzo goodies)

$result = Arrays::map($objects, Functions::extract()->id);

Check out: http://ouzo.readthedocs.org/en/latest/utils/functions.html#extract

See also functional programming with ouzo (I cannot post a link).

woru
  • 1,420
  • 9
  • 17
0

You can also use extract_property() which is a well tested library designed specifically for this job (disclaimer: I am the author).

Duru Can Celasun
  • 1,621
  • 1
  • 16
  • 28
0

Did you try the array_keys function?

EDIT:

<?php 
   $ids = array();
   for($c=0; $c<count($the_array); $c++) $ids[$c] = $the_array[$c]->id;
?>
bitfox
  • 2,281
  • 1
  • 18
  • 17
  • In the example, `array_keys` would return `[0,1,2]` not `[1,2,7]`. But I guess I could store the IDs as array keys... – DisgruntledGoat Jun 24 '11 at 11:21
  • Yes, that's right. I think that the iteration is mandatory. If you wouldn't like to iterate, the use of functions like array_map will do it for you in any case and this will incur in unnecessary overhead caused by calling the callback function (as reported by @webbiedave). I'm editing my post by adding the "obvious solution". – bitfox Jun 24 '11 at 13:39