Hi everyone,
I would like to propose a new function for PHP: array_search_range().
The RFC for this proposal can be found here:
https://wiki.php.net/rfc/array_search_range
The main motivation is to provide a memory-efficient way to search within a
specific range of an array, avoiding the overhead of creating intermediate
arrays with array_slice().
I appreciate your feedback and discussion on this proposal.
Best regards,
Sepehr Mahmoudi
Hi Sepehr,
PHP 8.6 is entering soft feature freeze now. It is impossible to promote a RFC
targeting 8.6. Therefore, IMO you should target 8.7 instead.
Weilin
Hi everyone,
The main motivation is to provide a memory-efficient way to search
within a specific range of an array, avoiding the overhead of creating
intermediate arrays witharray_slice().
Have you thought about expanding this idea to include other functions
with similar jobs (e.g. array_find, array_find_key, array_all, array_any)?
Hi everyone,
The main motivation is to provide a memory-efficient way to search within a specific range of an array, avoiding the overhead of creating intermediate arrays with
array_slice().Have you thought about expanding this idea to include other functions with similar jobs (e.g. array_find, array_find_key, array_all, array_any)?
I think it would be better to design something more composable - that is, a way to create a "lazy array slice", and then accept that in functions which can use it safely.
Swift, for instance, has both ArraySlice and Substring types which let you refer to a subset of an array or string without copying any data: https://developer.apple.com/documentation/swift/arrayslice https://developer.apple.com/documentation/swift/substring
It's conceptually similar to using an Iterator instead of filling an array with a range of values.
Regards,
Rowan Tommins
[IMSoP]
Hi Rowan,
That's a really interesting idea, and I completely agree with you.
A lazy array slice type would be a big step forward for PHP. It could
open the door to a lot of more composable and memory-friendly APIs.
I think it's a bigger topic than this RFC, so I'd love to explore it
properly in a separate proposal. For now, I'll keep array_search_range()
focused on its simple use case, and I'm happy to help shape the lazy
slice idea when the time comes.
Thanks for the thoughtful feedback!
Sepehr
On پنجشنبه ۱۳ اوت ۲۰۲۶، ۱۹:۰۸ Rowan Tommins [IMSoP] imsop.php@rwec.co.uk
wrote:
Hi everyone,
The main motivation is to provide a memory-efficient way to search
within a specific range of an array, avoiding the overhead of creating
intermediate arrays witharray_slice().Have you thought about expanding this idea to include other functions
with similar jobs (e.g. array_find, array_find_key, array_all, array_any)?I think it would be better to design something more composable - that is,
a way to create a "lazy array slice", and then accept that in functions
which can use it safely.Swift, for instance, has both ArraySlice and Substring types which let you
refer to a subset of an array or string without copying any data: <
https://developer.apple.com/documentation/swift/arrayslice> <
https://developer.apple.com/documentation/swift/substring>It's conceptually similar to using an Iterator instead of filling an array
with a range of values.Regards,
Rowan Tommins
[IMSoP]
Hi Sepehr,
I think it's a bigger topic than this RFC, so I'd love to explore it
properly in a separate proposal. For now, I'll keep array_search_range()
focused on its simple use case, and I'm happy to help shape the lazy
slice idea when the time comes.
While I appreciate that it's a harder feature to design and implement, I don't think it makes sense to treat it as a separate discussion, because if we add an ArraySlice type then array_search_range would immediately become redundant:
array_search_range('target', $arr, 2, 10, true)
Would be the same as:
array_search('target', new ArraySlice($arr, 2, 10), true)
Or maybe even:
array_search('target', $arr[2..10], true)
So I think it's only worth adding array_search_range if we think a more general solution is so hard that it will never happen, or not happen for many years.
PS: The convention on this list is to post replies below the (edited) text you're replying to, like I've done here, rather than above it.
Thanks,
Rowan Tommins
[IMSoP]
Hi Rowan,
Thanks for your reply. I understand your point, but I think it's worth
looking at this from a practical perspective.
Right now, ArraySlice doesn't exist in PHP. It's an interesting idea,
but it has no RFC, no implementation, and no timeline. We don't know
when or if it will happen.
array_search_range() solves a real problem today. It's simple, it's
focused, and it follows existing PHP conventions like array_slice().
If ArraySlice is ever introduced, I'd be happy to revisit
array_search_range() and discuss whether it's still needed. But
blocking a useful function on a hypothetical future feature doesn't
help users who need it now.
The two proposals aren't in competition. One is a small, practical
improvement that can land quickly. The other is a larger architectural
change that needs much more discussion and design work.
I think both can move forward independently.
Best,
Sepehr
On جمعه ۱۴ اوت ۲۰۲۶، ۱۷:۵۴ Rowan Tommins [IMSoP] imsop.php@rwec.co.uk
wrote:
Hi Sepehr,
On 13 August 2026 17:31:39 BST, "سپهر محمودی" sepehrphpr@gmail.com
wrote:I think it's a bigger topic than this RFC, so I'd love to explore it
properly in a separate proposal. For now, I'll keep array_search_range()
focused on its simple use case, and I'm happy to help shape the lazy
slice idea when the time comes.While I appreciate that it's a harder feature to design and implement, I
don't think it makes sense to treat it as a separate discussion, because if
we add an ArraySlice type then array_search_range would immediately become
redundant:array_search_range('target', $arr, 2, 10, true)
Would be the same as:
array_search('target', new ArraySlice($arr, 2, 10), true)
Or maybe even:
array_search('target', $arr[2..10], true)
So I think it's only worth adding array_search_range if we think a more
general solution is so hard that it will never happen, or not happen for
many years.PS: The convention on this list is to post replies below the (edited) text
you're replying to, like I've done here, rather than above it.Thanks,
Rowan Tommins
[IMSoP]
Hi Sepehr,
Right now, ArraySlice doesn't exist in PHP. It's an interesting idea,
but it has no RFC, no implementation, and no timeline. We don't know
when or if it will happen.
I understand what you mean, but it feels quite premature to make that judgement - this discussion has been going for less than two days, and there's a whole year to get a feature into 8.7 (it's already too late for 8.6).
I don't know the details of the implementation of either feature, but it doesn't feel like there's that much extra. At the core, you need some code somewhere that takes an array and two ints, and iterates that slice of the array.
At heart, an ArraySlice object would just be a holder for those three values, plus a bit of code to iterate over that subset. Custom syntax would be nice, but easy to add later.
Thinking about it, it ought to be trivial to build something on top of ArrayIterator, which already has a seek() method to jump to the start point. Which brings back a frequent debate about how the array_* functions should interact with iterators in general - should it actually be:
iter_search(new ArraySliceIterator($arr, 10, 42), true)
I think it's worth exploring these different options, if only to rule them out as too difficult.
(PS Again: please post replies below quoted text, not above it.)
Regards,
Rowan Tommins
[IMSoP]
At heart, an ArraySlice object would just be a holder for those three values, plus a bit of code to iterate over that subset. Custom syntax would be nice, but easy to add later.
Thinking about it, it ought to be trivial to build something on top of ArrayIterator, which already has a seek() method to jump to the start point. Which brings back a frequent debate about how the array_* functions should interact with iterators in general - should it actually be:
iter_search(new ArraySliceIterator($arr, 10, 42), true)
LimitIterator (https://www.php.net/manual/en/class.limititerator.php) more or less does this. But as you said, you cannot pass this to array_search, because that expects an array.
Regards,
Sjoerd
Hi Sjoerd,
Thanks for pointing that out. You're absolutely right - LimitIterator
does provide similar functionality, but as you noted, it can't be
passed to array_search() or other array_* functions.
This is exactly the gap array_search_range() fills. It gives users a
simple, direct way to search within a range of an array without
needing to convert to iterators, manually loop, or create temporary
arrays with array_slice().
Best,
Sepehr
On جمعه ۱۴ اوت ۲۰۲۶، ۲۳:۲۴ Sjoerd Langkemper sjoerd-php@linuxonly.nl
wrote:
At heart, an ArraySlice object would just be a holder for those three
values, plus a bit of code to iterate over that subset. Custom syntax would
be nice, but easy to add later.Thinking about it, it ought to be trivial to build something on top of
ArrayIterator, which already has a seek() method to jump to the start
point. Which brings back a frequent debate about how the array_* functions
should interact with iterators in general - should it actually be:iter_search(new ArraySliceIterator($arr, 10, 42), true)
LimitIterator (https://www.php.net/manual/en/class.limititerator.php)
more or less does this. But as you said, you cannot pass this to
array_search, because that expects an array.Regards,
Sjoerd