[GIT-PULLS] [php-src] PR #23325: Add array_search_range() function

[email protected] (sepehrphpr)
Newsgroups php.git-pulls
Message-ID <[email protected]>
Pull Request: https://github.com/php/php-src/pull/23325
Author: sepehrphpr

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"
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.