(PHP 5 >= 5.1.0)
iterator_apply — Call a function for every element in an iterator
Calls a function for every element in an iterator.
The class to iterate over.
The callback function to call on every element.
Hinweis: The function must return TRUE in order to continue iterating over the iterator.
Arguments to pass to the callback function.
Returns the iteration count.
Beispiel #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));
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
APPLES BANANAS CHERRIES
To clarify, this method does not work exactly like array_walk(), since the current key/value of the iterator is not passed to the callback function.
This php method is equivalent to:
<?php
function iterator_apply(Traversable $iterator, $function, array $args)
{
$count = 0;
foreach ($iterator as $ignored)
{
call_user_func_array($function, $args);
$count++;
}
return $count;
}
?>