| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Improved; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Get the first element that matches a condition. |
||
| 9 | * Returns null if no element is found. |
||
| 10 | * |
||
| 11 | * @param iterable $iterable |
||
| 12 | * @param callable $matcher |
||
| 13 | * @return mixed |
||
| 14 | */ |
||
| 15 | function iterable_find(iterable $iterable, callable $matcher) |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 16 | { |
||
| 17 | 18 | foreach ($iterable as $key => $value) { |
|
| 18 | 17 | if ((bool)call_user_func($matcher, $value, $key)) { |
|
| 19 | 11 | return $value; |
|
| 20 | } |
||
| 21 | } |
||
| 22 | |||
| 23 | 7 | return null; |
|
| 24 | } |
||
| 25 |