Base de conocimientosCentro de exámenes
ESEspañol
Elegir idioma13

CKTIS / PHP

IteratorIterator

Adapta una fuente Traversable al cursor Iterator.

Versión: PHP 8.4; repository runtime, production runtime not attested

Sintaxis

new IteratorIterator($traversable)

Adapta una fuente Traversable al cursor Iterator. Sirve para IteratorAggregate cuando se necesitan current, next y key. Antes de leer manualmente llama a rewind en el adaptador. No se copian los datos por anticipado a un 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]

Ejemplos

<?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]

Fuentes