Finding

find

Definition

1
Arr::find(array|Iterator|IteratorAggregate $array, callable $condition, string $return = self::FIND_RETURN_VALUE): mixed|mixed[]
Copied!

Description

Find array (or iterable object) element(s) that match specified condition.

Modes ($return method argument)

Constant name
Description
FIND_RETURN_VALUE
Return first value of an array that match find condition
FIND_RETURN_KEY
Return first key of an array that match find condition
FIND_RETURN_ALL
Return array of all values (preserving original keys) that match find condition

Examples

1
$array = [
2
'a' => 0,
3
'b' => 1,
4
3 => 'c',
5
4 => 5
6
];
7
8
9
Arr::find($array, 'boolval') -> 1
10
Arr::find($array, function ($element) {
11
return is_string($element);
12
}) -> 'c'
13
14
15
Arr::find($array, 'boolval', Arr::FIND_RETURN_KEY) -> 'b'
16
Arr::find($array, function ($element) {
17
return is_string($element);
18
}, Arr::FIND_RETURN_KEY) -> 3
19
20
21
Arr::find($array, 'boolval', Arr::FIND_RETURN_ALL) ->
22
[
23
'b' => 1,
24
3 => 'c',
25
4 => 5
26
]
27
28
Arr::find($array, function ($element) {
29
return is_string($element);
30
}, Arr::FIND_RETURN_ALL) ->
31
[
32
3 => 'c',
33
]
34
35
Arr::find($array, function ($element) {
36
return is_number($element);
37
}, Arr::FIND_RETURN_ALL) ->
38
[
39
'a' => 0,
40
'b' => 1,
41
4 => 5
42
]
Copied!
Last modified 1yr ago
Copy link