[PHP-DEV] [RFC] array_search_range() function
سپهر محمودی <[email protected]>
| Newsgroups | gmane.comp.php.devel |
|---|---|
| Message-ID | <CA+Jcf5thgC15hJ=Eq66suXDf-+2cjTT_+4Z-ecDdK7-5oVYp2Q@mail.gmail.com> |
Hello internals,
I'd like to open discussion on a new RFC proposing the
`array_search_range()` function.
This function extends `array_search()` by allowing the search to be
restricted to a specific range of the array, using an `$offset` and
`$length` parameter — similar in spirit to how `array_slice()` works.
RFC: https://wiki.php.net/rfc/array_search_range
Implementation: https://github.com/php/php-src/pull/23325
Proposed signature:
function array_search_range(mixed $needle, array $haystack, int $offset
= 0, ?int $length = null, bool $strict = false): int|false
The main motivation is to avoid the overhead of slicing a large array just
to search within a known sub-range, which is a common pattern when working
with pagination or windowed processing.
Example:
$arr = ['a', 'b', 'c', 'd', 'e'];
var_dump(array_search_range('c', $arr, 1, 3)); // 2
var_dump(array_search_range('a', $arr, 3, 3)); // false
The RFC is now Under Discussion. I welcome your feedback, questions, and
suggestions.
Best regards,
Sepehr