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

CKTIS / PHP

IteratorIterator

Adapta uma origem Traversable ao cursor Iterator.

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

Sintaxe

new IteratorIterator($traversable)

Adapta uma origem Traversable ao cursor Iterator. É útil com IteratorAggregate quando são necessários current, next e key. Antes de ler manualmente faça rewind no adaptador. Os dados não são copiados antecipadamente para um array.

class Source implements IteratorAggregate { public function getIterator(): Traversable {yield "x"=>4;yield "y"=>8;} }
$it=new IteratorIterator(new Source()); $it->rewind(); $first=$it->key(); $it->next(); echo json_encode([$first,$it->key(),$it->current()]);
["x","y",8]

Exemplos

<?php
class Source implements IteratorAggregate { public function getIterator(): Traversable {yield "x"=>4;yield "y"=>8;} }
$it=new IteratorIterator(new Source()); $it->rewind(); $first=$it->key(); $it->next(); echo json_encode([$first,$it->key(),$it->current()]);

Resultado

["x","y",8]

Fontes