Laravel -- Arr数组处理类
2018-10-29 本文已影响151人
爱折腾的傻小子
//> 判断 $value 是否是 数组 或 ArrayAccess 的实例对象
/**
* Determine whether the given value is array accessible.
*
* @param mixed $value
* @return bool
*/
public static function accessible($value)
{
return is_array($value) || $value instanceof ArrayAccess;
}
//> is_array() 和 instanceof 返回 bool(true | false)
# ArrayAccess 接口类
//> 向指定数组中添加key-value一对键值
/**
* Add an element to an array using "dot" notation if it doesn't exist.
*
* @param array $array
* @param string $key
* @param mixed $value
* @return array
*/
public static function add($array, $key, $value)
{
if (is_null(static::get($array, $key))) {
static::set($array, $key, $value);
}
return $array;
}
//> 使用举例 ---- 如果key值,在array中存在,返回array;如果不存在,添加key-value键值对
$arr = [
'name' => [
'one' => 'One',
'tow' => 'Tow'
],
'sex' => 'man',
'age' => 12,
];
dump(Arr::add($arr,'name.three','bms'));
# 打印数据后的数据
$arr = [
'name' => [
'one' => 'One',
'tow' => 'Tow',
'three' => 'bms'
],
'sex' => 'man',
'age' => 12,
];
//> 获取给定key在给定array中的值,如果不存在,默认返回null
/**
* Get an item from an array using "dot" notation.
*
* @param \ArrayAccess|array $array
* @param string $key
* @param mixed $default
* @return mixed
*/
public static function get($array, $key, $default = null)
{
if (! static::accessible($array)) {
return value($default);
}
if (is_null($key)) {
return $array;
}
if (static::exists($array, $key)) {
return $array[$key];
}
foreach (explode('.', $key) as $segment) {
if (static::accessible($array) && static::exists($array, $segment)) {
$array = $array[$segment];
} else {
return value($default);
}
}
return $array;
}
//> 这里采用 . 代替 [] (数组的标识符)
//> 设置key-value一对键值对,添加入array数组中;如果存在key时,改为更新
/**
* Set an array item to a given value using "dot" notation.
*
* If no key is given to the method, the entire array will be replaced.
*
* @param array $array
* @param string $key
* @param mixed $value
* @return array
*/
public static function set(&$array, $key, $value)
{
if (is_null($key)) {
return $array = $value;
}
$keys = explode('.', $key);
while (count($keys) > 1) {
$key = array_shift($keys);
// If the key doesn't exist at this depth, we will just create an empty array
// to hold the next value, allowing us to create the arrays to hold final
// values at the correct depth. Then we'll keep digging into the array.
if (! isset($array[$key]) || ! is_array($array[$key])) {
$array[$key] = [];
}
$array = &$array[$key];
}
$array[array_shift($keys)] = $value;
return $array;
}
//> 举例 ----
$arr = [
'name' => [
'one' => 'One',
'tow' => 'Tow'
],
'sex' => 'man',
'age' => 12,
];
dump(Arr::set($arr,'name.one','t'));
# set 返回的是最后一个数组的值;我们可以看出set方法,采用的是&引用赋值,直接打印$arr就可以得到全部的改变的数据
# [
"one" => "t"
"tow" => "Tow"
]
dump(Arr::set($arr,'name.three','bms'));
# [
"one" => "t"
"tow" => "Tow"
"three" => "bms"
]
//> 随机打乱一个数组
/**
* Shuffle the given array and return the result.
*
* @param array $array
* @return array
*/
public static function shuffle($array)
{
shuffle($array);
return $array;
}
//> 使用回调函数过滤数组
/**
* Filter the array using the given callback.
*
* @param array $array
* @param callable $callback
* @return array
*/
public static function where($array, callable $callback)
{
return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH);
}
//> 使用举例 ----
$arr = [
'name' => [
'one' => 'One',
'tow' => 'Tow'
],
'sex' => 'man',
'age' => 12,
];
dump(Arr::where($arr,function ($value,$key){
if( $key == 'sex' ){
return false;
}
if( $value % 2 == 0 ){
return true;
}
return true;
}));
# 打印结果 ----
"name" => array:2 [
"one" => "One"
"tow" => "Tow"
] "age" => 12
//> 数组排序,支持回调函数(排序规则:根据数组的值,进行排序)
/**
* Sort the array using the given callback or "dot" notation.
*
* @param array $array
* @param callable|string $callback
* @return array
*/
public static function sort($array, $callback)
{
return Collection::make($array)->sortBy($callback)->all();
}
//> 使用举例 ----
$arr = [
'name' => [
'one' => 'One',
'tow' => 'Tow'
],
'sex' => 'man',
'age' => 12,
];
$sortArr = Arr::sort($arr,function ($value,$key){
if( $key == 'name' ){
return 'a';
}
if( $key == 'age' ){
return 'z';
}
return 'g';
});
dump($sortArr);
# 打印数据
[
"name" => array:2 [
"one" => "One"
"tow" => "Tow"
]
"sex" => "man" "age" => 12
]
# 根据sortBy()方法,貌似回调函数返回的是排序字符串
//> 数组排序函数:根据 Key 值进行排序
/**
* Recursively sort an array by keys and values.
*
* @param array $array
* @return array
*/
public static function sortRecursive($array)
{
foreach ($array as &$value) {
if (is_array($value)) {
$value = static::sortRecursive($value);
}
}
if (static::isAssoc($array)) {
ksort($array);
} else {
sort($array);
}
return $array;
}
# 使用举例 -----
$arr = [
'name' => [
'one' => 'One',
'tow' => 'Tow'
],
'sex' => 'man',
'age' => 12,
];
dump(Arr::sortRecursive($arr));
# 打印结果
[
"age" => 12
"name" => array:2 [
"one" => "One"
"tow" => "Tow"
]
"sex" => "man"
]
//> 从 指定数组中 获取指定的值,并从该数组中移除该key
/**
* Get a value from the array, and remove it.
*
* @param array $array
* @param string $key
* @param mixed $default
* @return mixed
*/
public static function pull(&$array, $key, $default = null)
{
$value = static::get($array, $key, $default);
static::forget($array, $key);
return $value;
}
//> 举例 ----
$arr = [
'name' => [
'one' => 'One',
'tow' => 'Tow'
],
'sex' => 'man',
'age' => 12,
];
dump(Arr::pull($arr,'name.tow',null));
dump($arr);
# 打印数据 ----
"Tow"
array:3 [
"name" => array:1 [
"one" => "One"
]
"sex" => "man"
"age" => 12
]