Get array of property in stdClass array in php

2016-07-04 php arrays laravel

I have and array of StdClass objects ass follows:

0 => {#491
+"venue_id": 5355
+"distance": 0.68115836642929
}
1 => {#490
+"venue_id": 5354
+"distance": 1.2191811657926
}
2 => {#494
+"venue_id": 5357
+"distance": 1.4241327433078
}

These results are being retrieved from a database query (using laravel)
How can get venues_ids in an array? The outcome would look like something like:

0=> 5355, 1=> 5354, 2=> 5357

I can think of iterating the stdClass but is there any built in function to achieve this?

Answers

You can use array_map():

$arr = array_map(function($x) { return $x->venue_id; }, $arr);

If you're using PHP 7+, array_column supports passing in an array of objects:

$venue_ids = array_column($array, 'venue_id')

Related