You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/Arr.php
+45-2Lines changed: 45 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,8 @@
10
10
11
11
useArrayAccess;
12
12
useInvalidArgumentException;
13
+
useIterator;
14
+
useIteratorAggregate;
13
15
useReflectionFunction;
14
16
useReflectionMethod;
15
17
useThrowable;
@@ -59,6 +61,19 @@ class Arr
59
61
*/
60
62
constUNPACK_PRESERVE_ARRAY = 8;
61
63
64
+
/**
65
+
* Return value of array element matching find condition
66
+
*/
67
+
constFIND_RETURN_VALUE = 'value';
68
+
/**
69
+
* Return key of array element matching find condition
70
+
*/
71
+
constFIND_RETURN_KEY = 'key';
72
+
/**
73
+
* Return all values (preserving original keys) of array elements matching find condition
74
+
*/
75
+
constFIND_RETURN_ALL = 'all';
76
+
62
77
privateconstAUTO_INDEX_KEY = '[]';
63
78
privateconstKEY_SEPARATOR = '.';
64
79
@@ -112,7 +127,7 @@ public static function has(array $array, $keys): bool
112
127
113
128
foreach ($keysArrayas$key) {
114
129
if (!array_key_exists($key, $tmp)) {
115
-
returnfalse;
130
+
returnfalse;
116
131
}
117
132
$tmp = $tmp[$key];
118
133
}
@@ -692,6 +707,34 @@ public static function groupObjects(array $objects, string $method, ...$args): a
692
707
return$return;
693
708
}
694
709
710
+
/**
711
+
* Find array (or iterable object) element(s) that match specified condition
712
+
*
713
+
* @param array|Iterator|IteratorAggregate $array
714
+
* @param callable $condition Callable accepting one argument (current array element value) and returning truthy or falsy value
715
+
* @param string $return What type of result should be returned after finding desired element(s)
716
+
* @return mixed|mixed[] Either key, value or assoc array containing keys and values for elements matching specified condition. Returns null if element was not found, or empty array if FIND_RETURN_ALL mode was used.
0 commit comments