Skip to content

Commit

Permalink
Add Arr::find()
Browse files Browse the repository at this point in the history
  • Loading branch information
giuscris committed Oct 24, 2024
1 parent 4d9737d commit 8d2325b
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions formwork/src/Utils/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,27 @@ public static function some(array $array, callable $callback): bool
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
*
Expand Down

0 comments on commit 8d2325b

Please sign in to comment.