iterator_apply
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
Call a function for every element in an iterator
Description
Calls a function for every element in an iterator.
Parameters
iteratorThe iterator object to iterate over.
callbackThe callback function to call on every element. This function only receives the given
args, so it is nullary by default. Ifcount($args) === 3, for instance, the callback function is ternary.NoteThe function must return true in order to continue iterating over the
iterator.argsAn
arrayof arguments; each element ofargsis passed to the callbackcallbackas separate argument.
Return Values
Returns the iteration count.
Examples
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