Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / PHP

IteratorIterator

Adatta una sorgente Traversable al cursore Iterator.

Versione: PHP 8.4; repository runtime, production runtime not attested

Sintassi

new IteratorIterator($traversable)

Adatta una sorgente Traversable al cursore Iterator. È utile con IteratorAggregate se servono current, next e key. Prima di leggere manualmente chiama rewind sull’adattatore. I dati non vengono copiati in anticipo in 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]

Esempi

<?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()]);

Risultato

["x","y",8]

Fonti