@@ -80,15 +80,41 @@ public static function getKeysArray($keys): array
8080 }
8181
8282 /**
83- * Check if array has specified keys
83+ * Check if specified (nested) key(s) exists in array
8484 *
8585 * @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)
9087 * @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()
92118 */
93119 public static function hasKeys (array $ array , $ keys , bool $ strict = false ): bool
94120 {
@@ -102,17 +128,28 @@ public static function hasKeys(array $array, $keys, bool $strict = false): bool
102128 return $ strict ? true : false ;
103129 }
104130
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+
105145 /**
106146 * Get nested element of an array or object implementing array access
107147 *
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
114151 * @return null|mixed
115- * @see \Minwork\Helper\ Arr::getKeysArray()
152+ * @see Arr::getKeysArray()
116153 */
117154 public static function getNestedElement ($ array , $ keys , $ default = null )
118155 {
@@ -130,15 +167,28 @@ public static function getNestedElement($array, $keys, $default = null)
130167 return $ array ;
131168 }
132169
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+
133184 /**
134185 * Set array element specified by keys to the desired value (create missing keys if necessary)
135186 *
136187 * @param array $array
137188 * @param mixed $keys Keys needed to access desired array element (for possible formats see getKeysArray method)
138189 * @param mixed $value Value to set
139190 * @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()
142192 */
143193 public static function setNestedElement (array $ array , $ keys , $ value ): array
144194 {
@@ -163,6 +213,35 @@ public static function setNestedElement(array $array, $keys, $value): array
163213 return $ result ;
164214 }
165215
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+
166245 /**
167246 * Converts map of keys concatenated by dot and corresponding values to multidimensional array
168247 *
@@ -468,10 +547,10 @@ public static function mapObjects(array $objects, string $method, ...$args): arr
468547 * Filter array values by preserving only those which keys are present in array obtained from $keys variable
469548 *
470549 * @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)
472551 * @param bool $exclude If values matching $keys should be excluded from returned array
473552 * @return array
474- * @see \Minwork\Helper\ Arr::getKeysArray()
553+ * @see Arr::getKeysArray()
475554 */
476555 public static function filterByKeys (array $ array , $ keys , bool $ exclude = false ): array
477556 {
@@ -592,10 +671,10 @@ public static function groupObjects(array $objects, string $method, ...$args): a
592671 * Order associative array according to supplied keys order
593672 * Keys that are not present in $keys param will be appended to the end of an array preserving supplied order.
594673 * @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)
596675 * @param bool $appendUnmatched If values not matched by supplied keys should be appended to the end of an array
597676 * @return array
598- * @see \Minwork\Helper\ Arr::getKeysArray()
677+ * @see Arr::getKeysArray()
599678 */
600679 public static function orderByKeys (array $ array , $ keys , bool $ appendUnmatched = true ): array
601680 {
@@ -617,7 +696,7 @@ public static function orderByKeys(array $array, $keys, bool $appendUnmatched =
617696 * @param mixed $keys Keys in format specified by getKeysArray method or null to perform sort using 0-depth keys
618697 * @param bool $assoc If sorting should preserve main array keys (default: true)
619698 * @return array New sorted array
620- * @see \Minwork\Helper\ Arr::getKeysArray()
699+ * @see Arr::getKeysArray()
621700 */
622701 public static function sortByKeys (array $ array , $ keys = null , bool $ assoc = true ): array
623702 {
@@ -944,8 +1023,8 @@ public static function shuffle(array $array): array
9441023 * @param int $A
9451024 * @param int $B
9461025 * @return array
947- * @see \Minwork\Helper\ Arr::even()
948- * @see \Minwork\Helper\ Arr::odd()
1026+ * @see Arr::even()
1027+ * @see Arr::odd()
9491028 */
9501029 public static function nth (array $ array , int $ A = 1 , int $ B = 0 ): array
9511030 {
0 commit comments