mirror of
https://github.com/getformwork/formwork.git
synced 2025-01-17 13:38:22 +01:00
Add Arr::find()
This commit is contained in:
parent
4d9737d2e3
commit
8d2325bc67
@ -399,6 +399,27 @@ class Arr
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find the first element of an array passing a test callback
|
||||||
|
*
|
||||||
|
* The key of each element is passed to the callback as second argument
|
||||||
|
*
|
||||||
|
* @template T of mixed
|
||||||
|
*
|
||||||
|
* @param array<T> $array
|
||||||
|
*
|
||||||
|
* @return ?T
|
||||||
|
*/
|
||||||
|
public static function find(array $array, callable $callback): mixed
|
||||||
|
{
|
||||||
|
foreach ($array as $key => $value) {
|
||||||
|
if ($callback($value, $key)) {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the value corresponding to the specified key from each element of an array
|
* Get the value corresponding to the specified key from each element of an array
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user