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
| Parameter | Type | What it takes |
|---|---|---|
| $iterator | Traversable | The iterator object to iterate over. |
| $callback | callable | The 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 | ?array | An 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
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.