Re: [RFC] array_search_range() function
| From: | سپهر محمودی | Date: | Mon, 17 Aug 2026 20:25:12 +0000 |
| Subject: | Re: [RFC] array_search_range() function | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-132239@lists.php.net to get a copy of this message | ||
Hi Rowan,
Thanks for the detailed and thoughtful feedback — I really appreciate it.
You make a great point. A “lazy array slice” (like Swift’s ArraySlice)
would indeed be a more general and composable solution, and it would
benefit array_find(), array_find_key(), array_all(), array_any() and
others — not just array_search().
That said, I see array_search_range() as a small, focused step that
solves a concrete and common problem today, without waiting for a much
larger design discussion.
I’d be genuinely interested in your opinion on the best path forward:
1.
Should we proceed with array_search_range() as a focused addition
now, and explore “lazy array slice” as a separate, broader RFC?
2.
Or do you think it would be better to fold this idea into the
larger concept from the start?
Either way, I’m happy to collaborate and learn from the discussion.
Regards,
Sepehr
On دوشنبه ۱۷ اوت ۲۰۲۶، ۲۲:۳۰ Larry Garfield <larry@garfieldtech.com>
wrote:
> On Mon, Aug 17, 2026, at 4:46 AM, سپهر محمودی wrote:
> > 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
>
> I agree with Rowan's comments in the previous thread. I'd rather see an
> efficient array-range syntax first, then array_search() will "just work" in
> this way. I don't see it as a common enough use case that it is worth its
> own dedicated special function.
>
> --Larry Garfield
>