This is an old revision of the document!
PHP RFC: array_find
- Version: 0.9
- Date: 2024-03-31
- Author: Joshua Rüsweg, josh@php.net
- Status: Draft
- First Published at: https://wiki.php.net/rfc/array_find
Introduction
This RFC proposes the addition of a new function array_find
, which returns the first element for which a predicate callback returns true
.
There are currently lots of functions in PHP for modifying or filtering arrays. However, there is no simple function to search an array with a function and return the first result. Implementing this in userland is relatively simple (either via a loop or via array_filter combined with reset), but the function is often required, so this type of function is often built [1]. I therefore see a reason to include this function as standard with the next PHP version. In addition, the implementation of the function is similar to array_filter
and relatively trivial to implement, so the maintenance effort should be low.
Proposal
Add a new function array_find
:
function array_find(array $array, callable $callback, bool $return_array_key = false): mixed {}
Parameters
array $array
The array that should be searched.
callable $callback
The callback function to call to check each element. The first parameter contains the value, the second parameter contains the corresponding key. If this function returns true, the value (or key) is returned from array_find
and the callback will not be called for further elements.
bool $return_array_key
If this parameter is true
, the array key is returned instead of the value.
Return Value
The function returns the first entry for which the $callback
returns true
. If the third parameter ($return_array_key
) is true
, the function returns the array index. Otherwise the function returns the corresponding array value. If no matching entry is found, the function returns NULL
Examples
$array = [ 'a' => 'dog', 'b' => 'cat', 'c' => 'cow', 'd' => 'duck', 'e' => 'goose', 'f' => 'elephant' ]; // Find the first animal with a name longer than 4 characters. var_dump(array_find($array, function (string $value) { return strlen($value) > 4; })); // string(5) "goose" // Find the first animal whose name begins with f. var_dump(array_find($array, function (string $value) { return str_starts_with($value, 'f'); })); // NULL // Find the key corresponding to the first animal with a name longer than 4 characters. var_dump(array_find($array, function (string $value) { return strlen($value) > 4; }, true)); // string(1) "e" // Find the first animal whose where the array key is the first symbol of the animal. var_dump(array_find($array, function (string $value, $key) { return $value[0] === $key; })); // string(3) "cow"
Backward Incompatible Changes
Functions created by the user and named array_find
lead to a PHP error with the new version.
Proposed PHP Version(s)
PHP 8.4
RFC Impact
To SAPIs
None.
To Existing Extensions
None.
To Opcache
None.
New Constants
None.
php.ini Defaults
None.
Open Issues
None.
Unaffected PHP Functionality
This RFC only adds a new function to PHP and only affects previously defined functions which are named as the proposed function.
Implementation
References
[1] GitHub currently has 656 results for array_find and Laravel implements this feature by default with array_first.