Hacker News new | ask | show | jobs
by bvrmn 342 days ago
What's idiomatic way to get index (0, n-1) with the value? Parent example shows you could not use $key as a generic solution.
1 comments

You can use the SPL LimitIterator, feed it the generator, and give it the offset and limit.

``` foreach (new LimitIterator($generator, 0, 3) as $value) { echo $value; } ```

https://www.php.net/manual/en/class.limititerator.php