Skip to content
Tutorials Center
My learning

Topics

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
<?php
function gen() { yield 1; yield 2; }
$a = iterator_to_array(gen(), false);   // [1, 2]

Try it

PHP The preserve_keys trap

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.

PHP Other Traversables and iterator_count

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

PHP
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 false for generators that yield without keys or repeat keys, such as yield from over several sources.
  • Everything is loaded into memory. The point of a generator is to avoid that. Use foreach when 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 than true. It does not pass the current item to the callback (you pass arguments through $args or read the iterator yourself), so foreach is almost always clearer.
  • Named argument: iterator_to_array($it, preserve_keys: false).