(PHP 5 >= 5.1.0, PHP 7)
iterator_apply — Call a function for every element in an iterator
iterator_apply ( Traversable $iterator , callable $function [, array $args = null ] ) : int
Calls a function for every element in an iterator.
iterator
The iterator object to iterate over.
function
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 theiterator
.
args
An array of arguments; each element of args
is passed to the callback function
as separate argument.
Returns the iteration count.
Example #1 iterator_apply() example
<?php 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)); ?>
The above example will output:
APPLES BANANAS CHERRIES
© 1997–2020 The PHP Documentation Group
Licensed under the Creative Commons Attribution License v3.0 or later.
https://www.php.net/manual/en/function.iterator-apply.php