@@ -80,15 +80,41 @@ public static function getKeysArray($keys): array
80
80
}
81
81
82
82
/**
83
- * Check if array has specified keys
83
+ * Check if specified (nested) key(s) exists in array
84
84
*
85
85
* @param array $array
86
- * @param mixed $keys
87
- * See getKeysArray method
88
- * @param bool $strict
89
- * If array must have all of specified keys
86
+ * @param mixed $keys Keys needed to access desired array element (for possible formats see getKeysArray method)
90
87
* @return bool
91
- * @see \Minwork\Helper\Arr::getKeysArray()
88
+ * @see Arr::getKeysArray()
89
+ */
90
+ public static function has (array $ array , $ keys ): bool
91
+ {
92
+ $ keysArray = self ::getKeysArray ($ keys );
93
+
94
+ if (empty ($ keysArray )) {
95
+ return false ;
96
+ }
97
+
98
+ $ tmp = $ array ;
99
+
100
+ foreach ($ keysArray as $ key ) {
101
+ if (!array_key_exists ($ key , $ tmp )) {
102
+ return false ;
103
+ }
104
+ $ tmp = $ tmp [$ key ];
105
+ }
106
+
107
+ return true ;
108
+ }
109
+
110
+ /**
111
+ * Check if array has list of specified keys
112
+ *
113
+ * @param array $array
114
+ * @param mixed $keys Keys needed to access desired array element (for possible formats see getKeysArray method)
115
+ * @param bool $strict If array must have all of specified keys
116
+ * @return bool
117
+ * @see Arr::getKeysArray()
92
118
*/
93
119
public static function hasKeys (array $ array , $ keys , bool $ strict = false ): bool
94
120
{
@@ -102,17 +128,28 @@ public static function hasKeys(array $array, $keys, bool $strict = false): bool
102
128
return $ strict ? true : false ;
103
129
}
104
130
131
+ /**
132
+ * Alias of Arr::getNestedElement
133
+ *
134
+ * @param array|ArrayAccess $array Array or object implementing array access to get element from
135
+ * @param mixed $keys Keys needed to access desired array element (for possible formats see getKeysArray method)
136
+ * @param mixed $default Default value if element was not found
137
+ * @return null|mixed
138
+ * @see Arr::getNestedElement()
139
+ */
140
+ public static function get ($ array , $ keys , $ default = null )
141
+ {
142
+ return self ::getNestedElement ($ array , $ keys , $ default );
143
+ }
144
+
105
145
/**
106
146
* Get nested element of an array or object implementing array access
107
147
*
108
- * @param array|ArrayAccess $array
109
- * Array or object implementing array access to get element from
110
- * @param mixed $keys
111
- * See getKeysArray method
112
- * @param mixed $default
113
- * Default value if element was not found
148
+ * @param array|ArrayAccess $array Array or object implementing array access to get element from
149
+ * @param mixed $keys Keys needed to access desired array element (for possible formats see getKeysArray method)
150
+ * @param mixed $default Default value if element was not found
114
151
* @return null|mixed
115
- * @see \Minwork\Helper\ Arr::getKeysArray()
152
+ * @see Arr::getKeysArray()
116
153
*/
117
154
public static function getNestedElement ($ array , $ keys , $ default = null )
118
155
{
@@ -130,15 +167,28 @@ public static function getNestedElement($array, $keys, $default = null)
130
167
return $ array ;
131
168
}
132
169
170
+ /**
171
+ * Alias of Arr::setNestedElement
172
+ *
173
+ * @param array $array
174
+ * @param mixed $keys Keys needed to access desired array element (for possible formats see getKeysArray method)
175
+ * @param mixed $value Value to set
176
+ * @return array Copy of an array with element set
177
+ * @see Arr::setNestedElement()
178
+ */
179
+ public static function set (array $ array , $ keys , $ value ): array
180
+ {
181
+ return self ::setNestedElement ($ array , $ keys , $ value );
182
+ }
183
+
133
184
/**
134
185
* Set array element specified by keys to the desired value (create missing keys if necessary)
135
186
*
136
187
* @param array $array
137
188
* @param mixed $keys Keys needed to access desired array element (for possible formats see getKeysArray method)
138
189
* @param mixed $value Value to set
139
190
* @return array Copy of an array with element set
140
- * @see \Minwork\Helper\Arr::getKeysArray
141
- * @see \Minwork\Helper\Arr::getKeysArray()
191
+ * @see Arr::getKeysArray()
142
192
*/
143
193
public static function setNestedElement (array $ array , $ keys , $ value ): array
144
194
{
@@ -163,6 +213,35 @@ public static function setNestedElement(array $array, $keys, $value): array
163
213
return $ result ;
164
214
}
165
215
216
+ /**
217
+ * Destroy variable inside array at path specified by keys
218
+ *
219
+ * @param array $array
220
+ * @param mixed $keys Keys needed to access desired array element (for possible formats see getKeysArray method)
221
+ * @return array
222
+ * @see Arr::getKeysArray()
223
+ */
224
+ public static function unset (array $ array , $ keys ): array
225
+ {
226
+ $ result = self ::clone ($ array );
227
+ $ keysArray = self ::getKeysArray ($ keys );
228
+
229
+ $ tmp = &$ result ;
230
+
231
+ while (count ($ keysArray ) > 1 ) {
232
+ $ key = array_shift ($ keysArray );
233
+ if (!is_array ($ tmp ) || !array_key_exists ($ key , $ tmp )) {
234
+ return $ result ;
235
+ }
236
+
237
+ $ tmp = &$ tmp [$ key ];
238
+ }
239
+ $ key = array_shift ($ keysArray );
240
+ unset($ tmp [$ key ]);
241
+
242
+ return $ result ;
243
+ }
244
+
166
245
/**
167
246
* Converts map of keys concatenated by dot and corresponding values to multidimensional array
168
247
*
@@ -468,10 +547,10 @@ public static function mapObjects(array $objects, string $method, ...$args): arr
468
547
* Filter array values by preserving only those which keys are present in array obtained from $keys variable
469
548
*
470
549
* @param array $array
471
- * @param mixed $keys See getKeysArray function
550
+ * @param mixed $keys Keys needed to access desired array element (for possible formats see getKeysArray method)
472
551
* @param bool $exclude If values matching $keys should be excluded from returned array
473
552
* @return array
474
- * @see \Minwork\Helper\ Arr::getKeysArray()
553
+ * @see Arr::getKeysArray()
475
554
*/
476
555
public static function filterByKeys (array $ array , $ keys , bool $ exclude = false ): array
477
556
{
@@ -592,10 +671,10 @@ public static function groupObjects(array $objects, string $method, ...$args): a
592
671
* Order associative array according to supplied keys order
593
672
* Keys that are not present in $keys param will be appended to the end of an array preserving supplied order.
594
673
* @param array $array
595
- * @param mixed $keys See getKeysArray method
674
+ * @param mixed $keys Keys needed to access desired array element (for possible formats see getKeysArray method)
596
675
* @param bool $appendUnmatched If values not matched by supplied keys should be appended to the end of an array
597
676
* @return array
598
- * @see \Minwork\Helper\ Arr::getKeysArray()
677
+ * @see Arr::getKeysArray()
599
678
*/
600
679
public static function orderByKeys (array $ array , $ keys , bool $ appendUnmatched = true ): array
601
680
{
@@ -617,7 +696,7 @@ public static function orderByKeys(array $array, $keys, bool $appendUnmatched =
617
696
* @param mixed $keys Keys in format specified by getKeysArray method or null to perform sort using 0-depth keys
618
697
* @param bool $assoc If sorting should preserve main array keys (default: true)
619
698
* @return array New sorted array
620
- * @see \Minwork\Helper\ Arr::getKeysArray()
699
+ * @see Arr::getKeysArray()
621
700
*/
622
701
public static function sortByKeys (array $ array , $ keys = null , bool $ assoc = true ): array
623
702
{
@@ -944,8 +1023,8 @@ public static function shuffle(array $array): array
944
1023
* @param int $A
945
1024
* @param int $B
946
1025
* @return array
947
- * @see \Minwork\Helper\ Arr::even()
948
- * @see \Minwork\Helper\ Arr::odd()
1026
+ * @see Arr::even()
1027
+ * @see Arr::odd()
949
1028
*/
950
1029
public static function nth (array $ array , int $ A = 1 , int $ B = 0 ): array
951
1030
{
0 commit comments