Converting Zend Db ResultSet to PHP Arrays with toArray()
Learn how Zend\\Db\\ResultSet\\ResultSet::toArray() turns query results into plain PHP arrays, what hydrator it uses, and when to avoid it due to memory concerns.
12 Dec 2025, 15:41 UTC

When to use Zend\Db\ResultSet\ResultSet::toArray()
Call toArray() on a valid Zend\Db\ResultSet\ResultSet to obtain a plain PHP array where each element represents a row. The method uses the ResultSet’s hydrator; if no hydrator is set, each row becomes a stdClass object with column names as properties.
Worked example
use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Sql;
use Zend\Db\ResultSet\ResultSet;
// Assume $adapter is a configured Zend\Db\Adapter\Adapter instance
$sql = new Sql($adapter);
$select = $sql->select()->from('users')->where->equalTo('status', 'active');
$statement = $sql->prepareStatementForSqlObject($select);
$result = $statement->execute();
// $result is a ResultSet object
$resultSet = new ResultSet();
// Optionally set a hydrator, e.g., ArrayHydrator for plain arrays
// $resultSet->setHydrator(new Zend\Stdlib\Hydrator\ArrayHydrator());
$rows = $resultSet->initialize($result)->toArray();
// $rows is now an array of stdClass objects (or hydrated arrays)
foreach ($rows as $row) {
echo $row->username . PHP_EOL;
}
How the mechanism works
- The ResultSet holds a reference to the executed statement.
- When
toArray()is invoked, it iterates over the statement, fetches each row, and passes the row data through the configured hydrator. - If the hydrator is
null, the default behavior creates astdClassinstance and assigns column values as properties. - The method returns a new PHP array containing all hydrated rows.
Limits and common mistakes
- Memory consumption –
toArray()materializes every row in memory. For large result sets consider pagination (LIMIT/OFFSET) or iterating withcurrent()/next()on the ResultSet instead. - Closed or invalid ResultSet – Calling
toArray()after the statement has been closed or after a previous fetch that exhausted the cursor throws aBadMethodCallException. Always ensure the ResultSet is initialized ($resultSet->initialize($result)) and not reused. - Hydrator mismatch – If you set a hydrator that expects specific object types (e.g., an ObjectPropertyHydrator for a User entity) but the ResultSet contains columns that do not map, you may get incomplete objects or exceptions. Verify the hydrator with
$resultSet->getHydrator()before conversion. - Unexpected data types – The hydrator does not perform type conversion; database values remain as strings unless the hydrator or your application casts them.
Performance considerations
Because toArray() builds the entire result set in memory, its time complexity is O(n) with a significant constant factor due to object hydration. For result sets under a few thousand rows the overhead is usually acceptable. Beyond that, consider using $resultSet->toArray() on a limited slice (LIMIT) or iterating with while ($row = $resultSet->current()) { … $resultSet->next(); } to keep memory usage flat.
Practical verification
After calling toArray(), you can check the result:
- Use
var_dump($rows);to inspect structure and data types. - Confirm row count with
count($rows);matches the expected number of rows from the query. - If a hydrator was set, verify that the first element is an instance of the expected class:
if ($rows[0] instanceof MyEntity) { … }.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.