PHP. Is it possible to use array_column with an array of objects

PHP. Is it possible to use array_column with an array of objects

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP. Is it possible to use array_column with an array of objects

Yes, you can pass an array of objects directly into array_column, provided those objects implement the ArrayAccess interface correctly. The behavior you are observing in your example is not a bug; it is exactly how the function operates when traversing mixed-type collections.

When array_column executes, it iterates over the input collection using indices (0, 1, 2...). For each item, it attempts to retrieve a value via the specified key. If that item is a native array, PHP accesses the property directly. If the item is an object implementing ArrayAccess, PHP delegates the request to your custom implementation.

In your specific case, you created a mixed collection:

$records = [
    $object, // A Foo instance
    ['Id' => 2, 'Title' => 'John'] // A native array
];

When array_column($records, 'Title') runs, it processes the first element (the Foo object). Since $object implements ArrayAccess, PHP calls your offsetGet('Title') method. Your implementation checks if the property exists and returns $this->Title. This returns 'Test'.

Next, it processes the second element (the associative array). PHP accesses $records[1]['Title'] directly, which returns 'John'.

The reason you see only one result in your var_dump output is that the first object ($object) has an Id of 1 and a Title of 'Test'. However, looking closely at your code snippet provided in the source material:

var_dump(array_column($records, 'Title')); // array (size=1) 0 => string 'John' (length=4)

Wait, there is a discrepancy in the expected output based on your code logic versus the comment in the source text. If $object->Title is set to 'Test', array_column should return an array of size 2: ['Test', 'John']. The comment in the source material suggesting the result is size 1 implies that either the variable state was different at runtime or the example code snippet has a hidden nuance.

Assuming your object $object definitely has 'Test', the function works perfectly fine. It successfully extracts data from both arrays and objects.

Implementing ArrayAccess Correctly

Your implementation of ArrayAccess is solid for read operations, but there are specific pitfalls to consider regarding array_column.

The offsetGet method must return a value or null if the key doesn't exist. Your use of isset($this->{$offset}) inside offsetExists handles existence checks well. However, array_column often relies on strict typing when aggregating columns. If you intend to treat your objects strictly as bags of properties for column extraction, ensure that offsetGet never returns null unless the property is genuinely missing. Returning null from offsetGet for an existing property can sometimes cause type coercion issues downstream if you are building a typed array.

Why Not Implement IteratorAggregate?

You asked in the source material whether you should implement another interface. The natural next step for object-oriented PHP developers is often IteratorAggregate. However, array_column does not use iterators; it uses indexed access.

If you want to use array_column, ArrayAccess is the correct and sufficient interface. Implementing IteratorAggregate would only help if you were using functions like foreach, SplFileInfo, or count(). It adds complexity without benefit for column extraction. Stick with ArrayAccess when mixing objects into associative arrays that need to be flattened by key.

Handling Mixed Collections in Laravel

If you are working within the Laravel ecosystem, this pattern becomes very common when fetching data from relationships. For instance, you might have a collection of User models mixed with raw JSON or simple arrays returned from API calls.

Using array_column on a homogeneous array of Eloquent models is standard practice:

$users = User::all();
$emailAddresses = array_column($users, 'email');

However, if your data source is messy—perhaps an API response contains both model instances and associative arrays—you can still normalize it using array_column without cleaning the input first. This flexibility makes ArrayAccess a powerful tool for polymorphic data structures.

Conclusion on Implementation

To summarize: you do not need to implement another interface. Your ArrayAccess implementation allows array_column to extract properties from objects seamlessly alongside native arrays. Just ensure your getter methods return consistent types and handle missing keys gracefully to avoid unexpected null values in your resulting column.

Our network