-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_find.php
162 lines (150 loc) · 5.71 KB
/
array_find.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/**
* Checks whether the $callback returns TRUE for ANY of the array
* elements.
*
* @param array $array The array that should be searched.
* @param callable $callback The callback function to call to check
* each element. The first parameter contains the value ($value), the
* second parameter contains the corresponding key ($key). If this
* function returns TRUE (or a truthy value), TRUE is returned
* immediately and the $callback will not be called for further
* elements.
*
* @return bool TRUE if there is at least one element for which
* $callback returns TRUE. FALSE otherwise.
*/
function array_any(array $array, callable $callback): bool {
foreach ($array as $key => $value) {
if ($callback($value, $key)) {
return true;
}
}
return false;
}
/**
* Checks whether the $callback returns TRUE for ALL the array
* elements.
*
* @param array $array The array that should be searched.
* @param callable $callback The callback function to call to check
* each element. The first parameter contains the value ($value), the
* second parameter contains the corresponding key. If this function
* returns FALSE (or any falsy value), FALSE is returned immediately
* and the $callback will not be called for further elements.
*
* @return bool TRUE, if $callback returns TRUE for all elements.
* FALSE otherwise.
*/
function array_all(array $array, callable $callback): bool {
foreach ($array as $key => $value) {
if (!$callback($value, $key)) {
return false;
}
}
return true;
}
if (\PHP_VERSION_ID >= 80000) {
/**
* Returns the VALUE of the first element from $array for which the
* $callback returns true. Returns NULL if no matching element is
* found.
*
* @param array $array The array that should be searched.
* @param callable $callback The callback function to call to check
* each element. The first parameter contains the value ($value),
* the second parameter contains the corresponding key ($key).
* If this callback returns TRUE (or a truthy value), the value
* ($value) is returned immediately and the callback will not be
* called for further elements.
*
* @return mixed The function returns the value of the first
* element for which the $callback returns TRUE. NULL, if no
* matching element is found. Note that the matching element value
* itself could be NULL as well.
*/
function array_find(array $array, callable $callback): mixed {
foreach ($array as $key => $value) {
if ($callback($value, $key)) {
return $value;
}
}
return null;
}
/**
* Returns the KEY of the first element from $array for which the
* $callback returns TRUE. If no matching element is found the
* function returns NULL.
*
* @param array $array The array that should be searched.
* @param callable $callback The callback function to call to check
* each element. The first parameter contains the value ($value),
* the second parameter contains the corresponding key ($key). If
* this function returns TRUE, the key ($key) is returned
* immediately and the callback will not be called for further
* elements.
*
* @return mixed The key of the first element for which the
* $callback returns TRUE. NULL, If no matching element is found.
*/
function array_find_key(array $array, callable $callback): mixed {
foreach ($array as $key => $value) {
if ($callback($value, $key)) {
return $key;
}
}
return null;
}
}
else {
/**
* Returns the VALUE of the first element from $array for which the
* $callback returns true. Returns NULL if no matching element is
* found.
*
* @param array $array The array that should be searched.
* @param callable $callback The callback function to call to check
* each element. The first parameter contains the value ($value),
* the second parameter contains the corresponding key ($key).
* If this callback returns TRUE (or a truthy value), the value
* ($value) is returned and the callback will not be called for
* further elements.
*
* @return mixed The function returns the value of the first
* element for which the $callback returns TRUE. NULL, if no
* matching element is found. Note that the matching element value
* itself could be NULL as well.
*/
function array_find(array $array, callable $callback) {
foreach ($array as $key => $value) {
if ($callback($value, $key)) {
return $value;
}
}
return null;
}
/**
* Returns the KEY of the first element for which the $callback
* returns TRUE. If no matching element is found the function
* returns NULL.
*
* @param array $array The array that should be searched.
* @param callable $callback The callback function to call to check
* each element. The first parameter contains the value ($value),
* the second parameter contains the corresponding key ($key). If
* this function returns TRUE, the key ($key) is returned
* immediately and the callback will not be called for further
* elements.
*
* @return mixed The key of the first element for which the
* $callback returns TRUE. NULL, If no matching element is found.
*/
function array_find_key(array $array, callable $callback) {
foreach ($array as $key => $value) {
if ($callback($value, $key)) {
return $key;
}
}
return null;
}
}