diff --git a/src/helper/Arr.php b/src/helper/Arr.php index ed4d6a9..4409f8c 100644 --- a/src/helper/Arr.php +++ b/src/helper/Arr.php @@ -32,9 +32,9 @@ public static function accessible($value) /** * Add an element to an array using "dot" notation if it doesn't exist. * - * @param array $array + * @param array $array * @param string $key - * @param mixed $value + * @param mixed $value * @return array */ public static function add($array, $key, $value) @@ -110,7 +110,7 @@ public static function divide($array) /** * Flatten a multi-dimensional associative array with dots. * - * @param array $array + * @param array $array * @param string $prepend * @return array */ @@ -132,7 +132,7 @@ public static function dot($array, $prepend = '') /** * Get all of the given array except for a specified array of keys. * - * @param array $array + * @param array $array * @param array|string $keys * @return array */ @@ -147,7 +147,7 @@ public static function except($array, $keys) * Determine if the given key exists in the provided array. * * @param \ArrayAccess|array $array - * @param string|int $key + * @param string|int $key * @return bool */ public static function exists($array, $key) @@ -162,9 +162,9 @@ public static function exists($array, $key) /** * Return the first element in an array passing a given truth test. * - * @param array $array + * @param array $array * @param callable|null $callback - * @param mixed $default + * @param mixed $default * @return mixed */ public static function first($array, callable $callback = null, $default = null) @@ -191,9 +191,9 @@ public static function first($array, callable $callback = null, $default = null) /** * Return the last element in an array passing a given truth test. * - * @param array $array + * @param array $array * @param callable|null $callback - * @param mixed $default + * @param mixed $default * @return mixed */ public static function last($array, callable $callback = null, $default = null) @@ -209,7 +209,7 @@ public static function last($array, callable $callback = null, $default = null) * Flatten a multi-dimensional array into a single level. * * @param array $array - * @param int $depth + * @param int $depth * @return array */ public static function flatten($array, $depth = INF) @@ -234,7 +234,7 @@ public static function flatten($array, $depth = INF) /** * Remove one or many array items from a given array using "dot" notation. * - * @param array $array + * @param array $array * @param array|string $keys * @return void */ @@ -279,8 +279,8 @@ public static function forget(&$array, $keys) * Get an item from an array using "dot" notation. * * @param \ArrayAccess|array $array - * @param string $key - * @param mixed $default + * @param string $key + * @param mixed $default * @return mixed */ public static function get($array, $key, $default = null) @@ -316,7 +316,7 @@ public static function get($array, $key, $default = null) * Check if an item or items exist in an array using "dot" notation. * * @param \ArrayAccess|array $array - * @param string|array $keys + * @param string|array $keys * @return bool */ public static function has($array, $keys) @@ -364,7 +364,7 @@ public static function isAssoc(array $array) /** * Get a subset of the items from the given array. * - * @param array $array + * @param array $array * @param array|string $keys * @return array */ @@ -376,8 +376,8 @@ public static function only($array, $keys) /** * Pluck an array of values from an array. * - * @param array $array - * @param string|array $value + * @param array $array + * @param string|array $value * @param string|array|null $key * @return array */ @@ -412,7 +412,7 @@ public static function pluck($array, $value, $key = null) /** * Explode the "value" and "key" arguments passed to "pluck". * - * @param string|array $value + * @param string|array $value * @param string|array|null $key * @return array */ @@ -447,9 +447,9 @@ public static function prepend($array, $value, $key = null) /** * Get a value from the array, and remove it. * - * @param array $array + * @param array $array * @param string $key - * @param mixed $default + * @param mixed $default * @return mixed */ public static function pull(&$array, $key, $default = null) @@ -464,7 +464,7 @@ public static function pull(&$array, $key, $default = null) /** * Get one or a specified number of random values from an array. * - * @param array $array + * @param array $array * @param int|null $number * @return mixed * @@ -506,9 +506,9 @@ public static function random($array, $number = null) * * If no key is given to the method, the entire array will be replaced. * - * @param array $array + * @param array $array * @param string $key - * @param mixed $value + * @param mixed $value * @return array */ public static function set(&$array, $key, $value) @@ -540,7 +540,7 @@ public static function set(&$array, $key, $value) /** * Shuffle the given array and return the result. * - * @param array $array + * @param array $array * @param int|null $seed * @return array */ @@ -562,7 +562,7 @@ public static function shuffle($array, $seed = null) /** * Sort the array using the given callback or "dot" notation. * - * @param array $array + * @param array $array * @param callable|string|null $callback * @return array */ @@ -608,7 +608,7 @@ public static function query($array) /** * Filter the array using the given callback. * - * @param array $array + * @param array $array * @param callable $callback * @return array */ @@ -631,4 +631,29 @@ public static function wrap($value) return is_array($value) ? $value : [$value]; } -} \ No newline at end of file + + public static function mergeDeep(array ...$arrays): array + { + $result = []; + foreach ($arrays as $array) { + foreach ($array as $key => $value) { + if (is_integer($key)) { + $result[] = $value; + } elseif (isset($result[$key]) && is_array($result[$key]) && is_array($value)) { + $result[$key] = self::mergeDeep( + $result[$key], + $value + ); + } else { + $result[$key] = $value; + } + } + } + return $result; + } + + public static function flatMap(callable $fn, array $array): array + { + return array_merge(...array_map($fn, $array)); + } +}