Base de conhecimentoCentro de Exames
PT · PTPortuguês (Portugal)
Escolher idioma13

CKTIS / PHP

iterator_apply()

Chama uma função ao percorrer um Traversable.

Versão: PHP 8.4; repository runtime, production runtime not attested

Sintaxe

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

Chama uma função ao percorrer um Traversable. O valor atual não é passado automaticamente, pelo que deve passar o iterador em args ou capturá-lo. true continua e false para. A contagem inclui a última chamada.

Assinatura

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

Saída do exemplo

[2,[2,4]]

Exemplos

<?php
$it = new ArrayIterator([2,4,6]); $seen = [];
$count = iterator_apply($it, function($cursor) use (&$seen) { $seen[] = $cursor->current(); return $cursor->current() < 4; }, [$it]);
echo json_encode([$count,$seen]);

Resultado

[2,[2,4]]

Fontes