3v4l.org

run code in 300+ PHP versions simultaneously
<?php $xml = simplexml_load_string(<<<EOD <x> <ml>1</ml> <ml>2</ml> <ml>3</ml> <ml>4</ml> <ml>5</ml> </x> EOD ); $gen = function() use ($xml) { $it = new IteratorIterator($xml->ml); $it->rewind(); $norewit = new NoRewindIterator($it); $count = 0; foreach ($norewit as $key => $value) { $count++; yield $key => "[$value]"; if ($count < 2) { continue; } $norewit->next(); yield from $norewit; } }; print_r(iterator_to_array($gen(), false));

preferences:
64.86 ms | 402 KiB | 5 Q