diff --git a/formwork/src/Utils/Arr.php b/formwork/src/Utils/Arr.php index bc3bf36a..78edaf07 100644 --- a/formwork/src/Utils/Arr.php +++ b/formwork/src/Utils/Arr.php @@ -399,6 +399,27 @@ class Arr 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 $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 *