PHP - Extracting a column of properties from an array of objects

array-column, arrays, object, php

Solution

If you have PHP 7.0 or later, the best way is to use the built in function `array_column()` to access a column of properties from an array of objects:

$idCats = array_column($cats, 'id');

But the son has to be an array or converted to an array

Problem

I've got an array of cats objects: ``` $cats = Array ( [0] => stdClass Object ( [id] => 15 ), [1] => stdClass Object ( [id] => 18 ), [2] => stdClass Object ( [id] => 23 ) ) ``` and I want to extract an array of cats' IDs in 1 line (not a function nor a loop). I was thinking about using `array_walk` with `create_function` but I don't know how to do it. Any idea?

Original source

Related problems