iterator_apply
Description
The iterator_apply of SPL for PHP calls a function for every element in an iterator.
Syntax
iterator_apply( Traversable $iterator, callable $callback, ?array $args = null ): int
Parameters
iterator
The iterator object to iterate over.
callback
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
An array of arguments; each element of args is passed to the callback callback as separate argument.
Return
Returns the iteration count.
Examples
1 · iterator callback args
<? function myfunction(Iterator $iterator) { echo $iterator->current() . PHP_EOL; return true; } $array = [ "a" => "one", "b" => "two", "c" => "three" ]; $iterator = new ArrayIterator($array); $callback = "myfunction"; $args = [ $iterator ]; $return = iterator_apply($iterator, $callback, $args); echo $return;
one two three 3