iterator_to_array()
iterator_to_array() — Copy the iterator into an array.
Built-in function of PHP 8.5. The manual lists it as available in PHP 5 >= 5.1.0, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
Signature
iterator_to_array(Traversable|array $iterator, bool $preserve_keys = true): array
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $iterator | Traversable|array | The iterator being copied. |
| $preserve_keys | bool | Whether to use the iterator element keys as index. |
Return value
An array containing the elements of the iterator.
Example
A runnable snippet:
$iterator = new ArrayIterator(array('recipe'=>'pancakes', 'egg', 'milk', 'flour')); var_dump(iterator_to_array($iterator, true)); var_dump(iterator_to_array($iterator, false));
Output:
array(4) { ["recipe"]=> string(8) "pancakes" [0]=> string(3) "egg" [1]=> string(4) "milk" [2]=> string(5) "flour" } array(4) { [0]=> string(8) "pancakes" [1]=> string(3) "egg" [2]=> string(4) "milk" [3]=> string(5) "flour" }
Changelog
| Version | Change |
|---|---|
| 8.2.0 | The type of iterator has been widened from Traversable to Traversable|array. |
All PHP 8.5 built-in functions
Reference card built from the PHP manual page iterator_to_array() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.