iterator_apply

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

iterator_applyCall a function for every element in an iterator

Опис

iterator_apply(Traversable $iterator, callable $callback, ?array $args = null): int

Calls a function for every element in an iterator.

Параметри

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.

Зауваження: 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.

Значення, що повертаються

Returns the iteration count.

Приклади

Приклад #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));
?>

Поданий вище приклад виведе:

APPLES
BANANAS
CHERRIES

Прогляньте також

  • array_walk() - Apply a user supplied function to every member of an array