3v4l.org

run code in 500+ PHP versions simultaneously
<?php class ArraySliceIterator implements IteratorAggregate { private LimitIterator $backingIterator; public function __construct(array $array, int $offset, int $limit) { $this->backingIterator = new LimitIterator( new ArrayIterator($array), $offset, $limit ); } public function getIterator(): Traversable { return $this->backingIterator; } } function iter_search(mixed $needle, iterable $haystack, bool $strict = false): int|string|false { foreach ( $haystack as $key => $value ) { echo "$key? "; if ( ( $strict && $value === $needle ) || ( ! $strict && $value == $needle ) ) { return $key; } } echo "\nNo more items :("; return false; } $bigArray = range(0, 10_000); $key = iter_search(1066, new ArraySliceIterator($bigArray, 1_000, 100)); echo "\nResult: "; var_dump($key); echo "\n\n----\n\n"; $key = iter_search(1966, new ArraySliceIterator($bigArray, 1_000, 100)); echo "\nResult: "; var_dump($key);

preferences:
42.15 ms | 869 KiB | 5 Q