Extracting Properties from Array of Objects in PHP
You have an array containing stdClass instances, each holding an id property. You want to extract those IDs into a plain indexed array without writing an explicit loop or defining a separate function. This is a common pattern when transforming data structures before passing them to APIs, serializing payloads, or feeding logic that expects flat lists.
In PHP 5.3 and later, the most direct solution uses array_column. It accepts three arguments: the source array, the column name (property), and optionally an index key. When applied to your example:
$cats = [
0 => (object) ['id' => 15],
1 => (object) ['id' => 18],
2 => (object) ['id' => 23],
];
$ids = array_column($cats, 'id');
// $ids is now [15, 18, 23]
This returns a numerically indexed array of values pulled from the id property of each object. It handles both arrays and objects uniformly because PHP treats object properties as map-like accessors under the hood. The function does not require you to define a callback or write any iteration logic yourself, satisfying the "one line" constraint.
If you are using an older PHP version (pre-5.3), array_column does not exist. In that case, array_map combined with get_object_vars or direct property access is the alternative:
$ids = array_map(function ($cat) {
return $cat->id;
}, $cats);
However, you mentioned wanting to avoid a function definition if possible. In modern PHP, you can use an anonymous inline arrow syntax if available (PHP 7.4+):
$ids = array_map(fn($c) => $c->id, $cats);
This keeps the code concise while remaining explicit about what is happening.
Another option exists if you are working with native arrays where the property name matches the key exactly: array_column works seamlessly there too. For instance:
$users = [
['name' => 'Alice', 'age' => 30],
['name' => 'Bob', 'age' => 25],
];
$ages = array_column($users, 'age'); // [30, 25]
This demonstrates that the approach generalizes beyond stdClass objects.
When to Use This Pattern
Extracting a single column from a collection of homogeneous objects is useful when:
- You need to pass a list of IDs to an API endpoint expecting a JSON array.
- You are building a dataset for further aggregation, such as calculating averages or grouping by another field.
- You want to avoid boilerplate loops in tight-coupled logic where readability matters.
Using array_column is preferable when your source is guaranteed to have the same structure across all elements. If some objects might lack the property or contain null values, array_column will include those nulls in the result unless you post-process them:
$ids = array_filter(array_column($cats, 'id')); // removes nulls and empty strings
This behavior is important to account for when constructing query parameters or sending requests where missing values could cause errors.
Performance Considerations
array_column operates in O(n) time, which is optimal for this task. It is implemented in C within the Zend engine, making it faster than equivalent PHP loops for large datasets. For very large arrays, avoid wrapping it in higher-order functions that add unnecessary overhead unless you need dynamic behavior.
If you are working inside a Laravel application, array_column remains valid and works with Eloquent collections as well:
$ids = $cats->pluck('id')->toArray(); // pluck is sugar for extracting a column
This integrates smoothly with Laravelâs collection methods, offering readable syntax while leveraging the same underlying extraction logic. For more advanced querying patterns, consider using pluck directly on Eloquent models or collections to keep your business logic clean and expressive.
Ultimately, if you need a one-liner to extract a property from an array of objects in modern PHP, array_column is the idiomatic choice. It is concise, efficient, and aligns with PHPâs functional programming capabilities without requiring custom callbacks or explicit loops.
