1、Arr::add()
如果给定的键在数组中不存在或给定的键的值被设置为 null ,那么 Arr::add 函数将会把给定的键值对添加到数组中:
use Illuminate\Support\Arr;
$array = Arr::add(['name' => 'Desk'], 'price', 100);
// ['name' => 'Desk', 'price' => 100]
$array = Arr::add(['name' => 'Desk', 'price' => null], 'price', 100);
// ['name' => 'Desk', 'price' => 100]
2、Arr::collapse()
将多个数组合并为一个数组:
use Illuminate\Support\Arr;
$array = Arr::collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
// [1, 2, 3, 4, 5, 6, 7, 8, 9]
3、Arr::dot()
将多维数组中所有的键平铺到一维数组中,新数组使用「.」符号表示层级包含关系:
use Illuminate\Support\Arr;
$array = ['products' => ['desk' => ['price' => 100]]];
$flattened = Arr::dot($array);
// ['products.desk.price' => 100]
4、Arr::except()
函数从数组中删除指定的键值对:
use Illuminate\Support\Arr;
$array = ['name' => 'Desk', 'price' => 100];
$filtered = Arr::except($array, ['price']);
// ['name' => 'Desk']
5、Arr::first()
函数返回数组中满足指定条件的第一个元素:
use Illuminate\Support\Arr;
$array = [100, 200, 300];
$first = Arr::first($array, function ($value, $key) {
return $value >= 150;
});
// 200
将默认值作为第三个参数传递给该方法,如果没有值满足条件,则返回该默认值:
use Illuminate\Support\Arr;
$first = Arr::first($array, $callback, $default);
6、Arr::only()
函数只返回给定数组中指定的键值对:
use Illuminate\Support\Arr;
$array = ['name' => 'Desk', 'price' => 100, 'orders' => 10];
$slice = Arr::only($array, ['name', 'price']);
// ['name' => 'Desk', 'price' => 100]
7、Arr::pluck()
函数从数组中检索给定键的所有值:
use Illuminate\Support\Arr;
$array = [
['developer' => ['id' => 1, 'name' => 'Taylor']],
['developer' => ['id' => 2, 'name' => 'Abigail']],
];
$names = Arr::pluck($array, 'developer.name');
// ['Taylor', 'Abigail']
你也可以指定结果的键:
use Illuminate\Support\Arr;
$names = Arr::pluck($array, 'developer.name', 'developer.id');
// [1 => 'Taylor', 2 => 'Abigail']
还可以给整个数组重新定义键
use Illuminate\Support\Arr;
$names = Arr::pluck($array,null, 'developer.id');
// ['1'=>['developer' => ['id' => 1, 'name' => 'Taylor']],'2'=>['developer' => ['id' => 2, 'name' => 'Abigail']]];
9、data_get()
函数使用「.」符号从多维数组或对象中根据指定键检索值:
$data = ['products' => ['desk' => ['price' => 100]]];
$price = data_get($data, 'products.desk.price');
// 100
也接受一个默认值,如果没有找到指定的键,将返回默认值
$discount = data_get($data, 'products.desk.discount', 0);
// 0
也可以使用 「*」 作为通配符匹配键名
$data = [
'product-one' => ['name' => 'Desk 1', 'price' => 100],
'product-two' => ['name' => 'Desk 2', 'price' => 150],
];
data_get($data, '*.name');
// ['Desk 1', 'Desk 2'];
10、data_set()
函数使用「.」符号给多维数组或对象赋值:
$data = ['products' => ['desk' => ['price' => 100]]];
data_set($data, 'products.desk.price', 200);
// ['products' => ['desk' => ['price' => 200]]]
这个函数也支持使用「*」作为通配符给相应键名赋值:
$data = [
'products' => [
['name' => 'Desk 1', 'price' => 100],
['name' => 'Desk 2', 'price' => 150],
],
];
data_set($data, 'products.*.price', 200);
/*
[
'products' => [
['name' => 'Desk 1', 'price' => 200],
['name' => 'Desk 2', 'price' => 200],
],
]
*/
通常情况下,已存在的值将会被覆盖。如果只是希望设置一个目前不存在的值,你可以增加一个 false 作为函数的第四个参数:
$data = ['products' => ['desk' => ['price' => 100]]];
data_set($data, 'products.desk.price', 200, false);
// ['products' => ['desk' => ['price' => 100]]]
参考资料:
https://learnku.com/docs/laravel/8.x/helpers/9393#method-data-fill