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;
}
?>
iterator_apply
(PHP 5 >= 5.1.0)
iterator_apply — Appelle une fonction pour tous les éléments d'un itérateur
Description
Appelle une fonction pour tous les éléments d'un itérateur.
Liste de paramètres
-
iterator -
La classe à itérer.
-
function -
La fonction à appeler à chaque élément.
Note: La fonction doit retourner
TRUEafin de continuer d'itérer à travers l'itérateur nommé par le paramètreiterator. -
args -
Les arguments à passer à la fonction de rappel.
Valeurs de retour
Retourne le nombre d'itération.
Exemples
Exemple #1 Exemple avec iterator_apply()
<?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));
?>
L'exemple ci-dessus va afficher :
APPLES BANANAS CHERRIES
kminkler at synacor dot com ¶
3 years ago
