8
8
9
9
class Util
10
10
{
11
- /**
12
- * returns the value for the given array key if it exists, otherwise, return the default
13
- * value
14
- *
15
- *@param string $key - the array key
16
- *@param array $arr - the array
17
- *@param mixed [$default=null] - the default value to return if otherwise
18
- *@return mixed
19
- */
20
- public static function value (string $ key , array $ arr , $ default = null )
21
- {
22
- if (array_key_exists ($ key , $ arr ))
23
- return $ arr [$ key ];
24
- else
25
- return $ default ;
26
- }
27
-
28
- /**
29
- * returns array value for the given array key if key exists and its value is an array
30
- * otherwise, return the default value
31
- *
32
- *@param string $key - the array key
33
- *@param array $arr - the array
34
- *@param array [$default=[]] - the default value to return if otherwise
35
- *@return array
36
- */
37
- public static function arrayValue (string $ key , array $ arr , array $ default = [])
38
- {
39
- if (array_key_exists ($ key , $ arr ) && is_array ($ arr [$ key ]))
40
- return $ arr [$ key ];
41
- else
42
- return $ default ;
43
- }
44
-
45
11
/**
46
12
* returns true if the given key is not set or if the key is set and it is truthy
47
13
*
@@ -86,18 +52,6 @@ public static function isNumeric($value)
86
52
return false ;
87
53
}
88
54
89
- /**
90
- * unsets an entry from an array if the key exists
91
- *
92
- *@param string $key - the array key
93
- *@param array $arr - the array
94
- */
95
- public static function unsetFromArray (string $ key , array &$ arr )
96
- {
97
- if (array_key_exists ($ key , $ arr ))
98
- unset($ arr [$ key ]);
99
- }
100
-
101
55
/**
102
56
* puts the value inside an array and returns the resulting array or returns the value if
103
57
* it is already an array
@@ -112,4 +66,63 @@ public static function makeArray($value)
112
66
else
113
67
return array ($ value );
114
68
}
69
+
70
+ /**
71
+ * returns the value for the first key in the keys array that exists in the array
72
+ * otherwise, return the default value
73
+ *
74
+ *@param string[]|string $keys - array of keys or a single string key
75
+ *@param array $arr - the array
76
+ *@param mixed [$default=null] - the default value to return if otherwise
77
+ *@return mixed
78
+ */
79
+ public static function value ($ keys , array $ arr , $ default = null )
80
+ {
81
+ $ keys = self ::makeArray ($ keys );
82
+ foreach ($ keys as $ key )
83
+ {
84
+ if (array_key_exists ($ key , $ arr ))
85
+ return $ arr [$ key ];
86
+ }
87
+
88
+ return $ default ;
89
+ }
90
+
91
+ /**
92
+ * returns the value for the first key in the keys array whose value is an array that
93
+ * exists in the array
94
+ * otherwise, return the default value
95
+ *
96
+ *@param string[]|string $keys - array of keys or a single string key
97
+ *@param array $arr - the array
98
+ *@param mixed [$default=[]] - the default value to return if otherwise
99
+ *@return array
100
+ */
101
+ public static function arrayValue ($ keys , array $ arr , array $ default = [])
102
+ {
103
+ $ keys = self ::makeArray ($ keys );
104
+ foreach ($ keys as $ key )
105
+ {
106
+ if (array_key_exists ($ key , $ arr ) && is_array ($ arr [$ key ]))
107
+ return $ arr [$ key ];
108
+ }
109
+
110
+ return $ default ;
111
+ }
112
+
113
+ /**
114
+ * unsets array of keys from the given array if it exists in the array
115
+ *
116
+ *@param string[]|string $keys - array of keys or a single string key
117
+ *@param array $arr - the array
118
+ */
119
+ public static function unsetFromArray ($ keys , array &$ arr )
120
+ {
121
+ $ keys = self ::makeArray ($ keys );
122
+ foreach ($ keys as $ key )
123
+ {
124
+ if (array_key_exists ($ key , $ arr ))
125
+ unset($ arr [$ key ]);
126
+ }
127
+ }
115
128
}
0 commit comments