PHP Reference ยท Array Functions
PHP iterator_to_array() function
Reference for PHP iterator_to_array(): turn a generator, ArrayIterator or any Traversable into an array, with the preserve_keys trap and iterator_count().
iterator_to_array() runs through any Traversable (a generator, an ArrayIterator, an IteratorAggregate and so on) and collects what it yields into a normal array. It is the bridge from lazy iteration back to the array functions, which do not accept iterators.
Beware the second parameter. By default the keys the iterator yields are kept, and when several items share a key the later ones overwrite the earlier ones. A plain generator numbers its keys, but yield from restarts the numbering for every inner generator, so keys collide and values vanish. When you only want the values, pass false.
<?php function gen() { yield 1; yield 2; } $a = iterator_to_array(gen(), false); // [1, 2]
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
iterator_to_array(Traversable $iterator, bool $preserve_keys = true): array iterator_count(Traversable $iterator): int iterator_apply(Traversable $iterator, callable $callback, ?array $args = null): int
| Name | Description |
|---|---|
$iterator |
The Traversable to read. Since PHP 8.2 arrays are accepted too (iterable). |
$preserve_keys |
true (default) uses the yielded keys; false produces a list with keys 0, 1, 2, .... |
Return value: the array of values. iterator_count() returns the number of items, and iterator_apply() returns how many items it visited.
Errors: a TypeError for a value that is neither array nor Traversable. With preserve_keys = true, an iterator that yields a key that is not an integer or a string (for example an object or an array) throws an error.
Usage notes
- Duplicate keys overwrite. Use
falsefor generators thatyieldwithout keys or repeat keys, such asyield fromover several sources. - Everything is loaded into memory. The point of a generator is to avoid that. Use
foreachwhen you can process the items one at a time. - Generators are single-use. After they finish, calling
iterator_to_array()again throws an exception ("Cannot traverse an already closed generator"). iterator_count()consumes the iterator too; it is not free for generators.iterator_apply()calls the callback once per item and stops early if the callback returns anything other thantrue. It does not pass the current item to the callback (you pass arguments through$argsor read the iterator yourself), soforeachis almost always clearer.- Named argument:
iterator_to_array($it, preserve_keys: false).