Znalostná bázaCentrum skúšok
SKSlovenčina
Vybrať jazyk13

CKTIS / PHP

IteratorIterator

Prispôsobí zdroj Traversable kurzoru Iterator.

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

Syntax

new IteratorIterator($traversable)

Prispôsobí zdroj Traversable kurzoru Iterator. Hodí sa pri IteratorAggregate, ak kód vyžaduje current, next a key. Pred ručným čítaním zavolajte na obale rewind. Údaje sa vopred nekopírujú do poľa.

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]

Príklady

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

Výsledok

["x","y",8]

Zdroje