Skip to content

Add array_search_range() function - #23325

Open
sepehrphpr wants to merge 1 commit into
php:masterfrom
sepehrphpr:array_search_range
Open

Add array_search_range() function#23325
sepehrphpr wants to merge 1 commit into
php:masterfrom
sepehrphpr:array_search_range

Conversation

@sepehrphpr

Copy link
Copy Markdown

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant