sepehrphpr ยท GitHub

Add array_search_range() function

This PR adds a new array_search_range() function to the PHP standard library.
Function signature
array_search_range(mixed $needle, array $haystack, int $offset = 0, ?int $length = null, bool $strict = false): int|string|false
Description

array_search_range() searches a portion of the array for a given value and returns the first corresponding key if successful, or false if not found.

It extends the existing array_search() function with two additional parameters:

$offset โ€” Start searching from this position (0-indexed). Negative values count from the end of the array.
$length โ€” Limit the search to this many elements after $offset. If null, searches to the end of the array.

Examples
$array = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 2, 'e' => 1];

array_search_range(2, $array); // "b"
array_search_range(2, $array, 2); // "d"
array_search_range(2, $array, 0, 2); // "b"
array_search_range(1, $array, -2); // "e"

Read the original on github.com โ†—