博主信息
雨天的小草的博客
博文
4
粉丝
0
评论
0
访问量
397
积分:0
P豆:8

Laravel一些好用的操作数组的助手函数分享

2022年08月05日 11:21:13阅读数:97博客 / 雨天的小草的博客/ Laravel框架

1、Arr::add()

如果给定的键在数组中不存在或给定的键的值被设置为 null ,那么 Arr::add 函数将会把给定的键值对添加到数组中:
  1. use Illuminate\Support\Arr;
  2. $array = Arr::add(['name' => 'Desk'], 'price', 100);
  3. // ['name' => 'Desk', 'price' => 100]
  4. $array = Arr::add(['name' => 'Desk', 'price' => null], 'price', 100);
  5. // ['name' => 'Desk', 'price' => 100]

2、Arr::collapse()

将多个数组合并为一个数组:
  1. use Illuminate\Support\Arr;
  2. $array = Arr::collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
  3. // [1, 2, 3, 4, 5, 6, 7, 8, 9]

3、Arr::dot()

将多维数组中所有的键平铺到一维数组中,新数组使用「.」符号表示层级包含关系:
  1. use Illuminate\Support\Arr;
  2. $array = ['products' => ['desk' => ['price' => 100]]];
  3. $flattened = Arr::dot($array);
  4. // ['products.desk.price' => 100]

4、Arr::except()

函数从数组中删除指定的键值对:
  1. use Illuminate\Support\Arr;
  2. $array = ['name' => 'Desk', 'price' => 100];
  3. $filtered = Arr::except($array, ['price']);
  4. // ['name' => 'Desk']

5、Arr::first()

函数返回数组中满足指定条件的第一个元素:
  1. use Illuminate\Support\Arr;
  2. $array = [100, 200, 300];
  3. $first = Arr::first($array, function ($value, $key) {
  4. return $value >= 150;
  5. });
  6. // 200
将默认值作为第三个参数传递给该方法,如果没有值满足条件,则返回该默认值:
  1. use Illuminate\Support\Arr;
  2. $first = Arr::first($array, $callback, $default);

6、Arr::only()

函数只返回给定数组中指定的键值对:
  1. use Illuminate\Support\Arr;
  2. $array = ['name' => 'Desk', 'price' => 100, 'orders' => 10];
  3. $slice = Arr::only($array, ['name', 'price']);
  4. // ['name' => 'Desk', 'price' => 100]

7、Arr::pluck()

函数从数组中检索给定键的所有值:
  1. use Illuminate\Support\Arr;
  2. $array = [
  3. ['developer' => ['id' => 1, 'name' => 'Taylor']],
  4. ['developer' => ['id' => 2, 'name' => 'Abigail']],
  5. ];
  6. $names = Arr::pluck($array, 'developer.name');
  7. // ['Taylor', 'Abigail']
你也可以指定结果的键:
  1. use Illuminate\Support\Arr;
  2. $names = Arr::pluck($array, 'developer.name', 'developer.id');
  3. // [1 => 'Taylor', 2 => 'Abigail']
还可以给整个数组重新定义键
  1. use Illuminate\Support\Arr;
  2. $names = Arr::pluck($array,null, 'developer.id');
  3. // ['1'=>['developer' => ['id' => 1, 'name' => 'Taylor']],'2'=>['developer' => ['id' => 2, 'name' => 'Abigail']]];

9、data_get()

函数使用「.」符号从多维数组或对象中根据指定键检索值:
  1. $data = ['products' => ['desk' => ['price' => 100]]];
  2. $price = data_get($data, 'products.desk.price');
  3. // 100
也接受一个默认值,如果没有找到指定的键,将返回默认值
  1. $discount = data_get($data, 'products.desk.discount', 0);
  2. // 0
也可以使用 「*」 作为通配符匹配键名
  1. $data = [
  2. 'product-one' => ['name' => 'Desk 1', 'price' => 100],
  3. 'product-two' => ['name' => 'Desk 2', 'price' => 150],
  4. ];
  5. data_get($data, '*.name');
  6. // ['Desk 1', 'Desk 2'];

10、data_set()

函数使用「.」符号给多维数组或对象赋值:
  1. $data = ['products' => ['desk' => ['price' => 100]]];
  2. data_set($data, 'products.desk.price', 200);
  3. // ['products' => ['desk' => ['price' => 200]]]
这个函数也支持使用「*」作为通配符给相应键名赋值:
  1. $data = [
  2. 'products' => [
  3. ['name' => 'Desk 1', 'price' => 100],
  4. ['name' => 'Desk 2', 'price' => 150],
  5. ],
  6. ];
  7. data_set($data, 'products.*.price', 200);
  8. /*
  9. [
  10. 'products' => [
  11. ['name' => 'Desk 1', 'price' => 200],
  12. ['name' => 'Desk 2', 'price' => 200],
  13. ],
  14. ]
  15. */
通常情况下,已存在的值将会被覆盖。如果只是希望设置一个目前不存在的值,你可以增加一个 false 作为函数的第四个参数:
  1. $data = ['products' => ['desk' => ['price' => 100]]];
  2. data_set($data, 'products.desk.price', 200, false);
  3. // ['products' => ['desk' => ['price' => 100]]]

参考资料:

https://learnku.com/docs/laravel/8.x/helpers/9393#method-data-fill

版权申明:本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!

全部评论

文明上网理性发言,请遵守新闻评论服务协议

条评论