CKTIS / PHP
InfiniteIterator
Cycle a rewindable inner Iterator repeatedly, keeping its keys on every pass.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
new InfiniteIterator($iterator)Cycle a rewindable inner Iterator repeatedly, keeping its keys on every pass. Stop after an explicit bound when the input is nonempty, for example with LimitIterator. An empty input produces no elements and a nonrewindable generator is unsuitable for repeated cycles.
$loop=new InfiniteIterator(new ArrayIterator(["p"=>"A","q"=>"B"]));
$sample=new LimitIterator($loop,0,5);
echo json_encode(iterator_to_array($sample,false));
["A","B","A","B","A"]
Examples
<?php
$loop=new InfiniteIterator(new ArrayIterator(["p"=>"A","q"=>"B"]));
$sample=new LimitIterator($loop,0,5);
echo json_encode(iterator_to_array($sample,false));
Result
["A","B","A","B","A"]