iterator_apply()

iterator_apply() — Call a function for every element in an iterator.

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_apply(Traversable $iterator, callable $callback, ?array $args = null): int

Parameters

ParameterTypeWhat it takes
$iteratorTraversableThe iterator object to iterate over.
$callbackcallableThe callback function to call on every element. This function only receives the given args, so it is nullary by default. If count($args) === 3, for instance, the callback function is ternary. Note: The function must return true in order to continue iterating over the iterator.
$args?arrayAn array of arguments; each element of args is passed to the callback callback as separate argument.

Return value

Returns the iteration count.

Example

A runnable snippet:

function print_caps(Iterator $iterator) { echo strtoupper($iterator->current()) . "\n"; return TRUE; } $it = new ArrayIterator(array("Apples", "Bananas", "Cherries")); iterator_apply($it, "print_caps", array($it));

Output:

APPLES BANANAS CHERRIES

See also

array_walk()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page iterator_apply() (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.