Skip to content

Commit

Permalink
增加Arr方法
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwuxin committed Aug 19, 2024
1 parent 52cb0be commit 84e64b7
Showing 1 changed file with 52 additions and 27 deletions.
79 changes: 52 additions & 27 deletions src/helper/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
*/
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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)
Expand All @@ -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
*
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand All @@ -631,4 +631,29 @@ public static function wrap($value)

return is_array($value) ? $value : [$value];
}
}

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));
}
}

0 comments on commit 84e64b7

Please sign in to comment.