PHP 8.5.0 RC4 available for testing

array_find

(PHP 8 >= 8.4.0)

array_findReturns the first element satisfying a callback function

Beschreibung

array_find(array $array, callable $callback): mixed

array_find() returns the value of the first element of an Array for which the given callback returns true. If no matching element is found the function returns null.

Parameter-Liste

array
The Array that should be searched.
callback

The callback function to call to check each element, which must be

callback(mixed $value, mixed $key): bool
If this function returns true, the value is returned from array_find() and the callback will not be called for further elements.

Rückgabewerte

The function returns the value of the first element for which the callback returns true. If no matching element is found the function returns null.

Beispiele

Beispiel #1 array_find() example

<?php
$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;
}));

// Find the first animal whose name begins with f.
var_dump(array_find($array, function (string $value) {
return
str_starts_with($value, 'f');
}));

// Find the first animal where the array key is the first symbol of the animal.
var_dump(array_find($array, function (string $value, $key) {
return
$value[0] === $key;
}));

// Find the first animal where the array key matching a RegEx.
var_dump(array_find($array, function ($value, $key) {
return
preg_match('/^([a-f])$/', $key);
}));
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

string(5) "goose"
NULL
string(3) "cow"
string(3) "dog"

Siehe auch

  • array_find_key() - Returns the key of the first element satisfying a callback function
  • array_all() - Checks if all Array elements satisfy a callback function
  • array_any() - Checks if at least one Array element satisfies a callback function
  • array_filter() - Filtert Elemente eines Arrays mittels einer Callback-Funktion
  • array_reduce() - Reduziert das Array mittels einer Callback-Funktion iterativ auf einen einzigen Wert
add a note

User Contributed Notes 2 notes

up
13
mail at nititech dot de
6 months ago
A simple fallback For older PHP versions, that do not have array_find:

<?php

/**
* Porting of PHP 8.4 function
*
* @template TValue of mixed
* @template TKey of array-key
*
* @param array<TKey, TValue> $array
* @param callable(TValue $value, TKey $key): bool $callback
* @return ?TValue
*
* @see https://www.php.net/manual/en/function.array-find.php
*/
function array_find(array $array, callable $callback): mixed
{
foreach (
$array as $key => $value) {
if (
$callback($value, $key)) {
return
$value;
}
}

return
null;
}
?>
up
0
harl at gmail dot com
5 hours ago
Note that if null satisfies the callback then there is no way to tell if null was returned because it was found in the array or if it was because nothing satisfying the callback was found.

In this case, it'll be more robust to use array_find_key; null can't be a key, so if that's what you get it must be because the search failed to find a match.

Obviously, you'd then use the array key to look up the corresponding value in the array.
To Top