3v4l.org

run code in 300+ PHP versions simultaneously
<?php function rotate(iterable $iterable, callable $predicate) { $tail = []; $match = false; foreach ($iterable as $key => $value) { if ($match || $predicate($value)) { yield $key => $value; $match = true; } else { $tail[] = [$key, $value]; } } foreach ($tail as [$key, $value]) { yield $key => $value; } } function gen() { yield 1; yield 2; yield 3; yield 4; yield 5; } $res = rotate(gen(), function($el) { return $el === 3; }); var_dump(iterator_to_array($res)); // [3, 4, 5, 1, 2]

preferences:
138.96 ms | 1952 KiB | 5 Q