Laravel - 数组函数

引入包

use Illuminate\Support\Arr;

一、检查给定的值是否可数组式访问

Arr::accessible(['a' => 1, 'b' => 2]);// true

二、数组添加 

Arr::add(['name' => 'Desk'], 'price', 100) ;// ['name' => 'Desk', 'price' => 100]
Arr::add(['name' => 'Desk', 'price' => null], 'price', 100);// ['name' => 'Desk', 'price' => 100] 若添加的键值存在并且值为null,则会将null改为修改的值

三、数组修改

$array = ['products' => ['desk' => ['price' => 100]]];
Arr::set($array, 'products.desk.price', 200); // ['products' => ['desk' => ['price' => 200]]]

四、 数组删除

         1.根据键值删除数组

Arr::except(['name' => 'Desk', 'price' => 100], ['price']);
// ['name' => 'Desk']

         2.从嵌套的数组中删除给定的键值对

Arr::forget(['products' => ['desk' => ['price' => 100]]], 'products.desk');
//['products' => []]

 五、查询

        1.判断数组中指定键值是否存在

Arr::exists(['name' => 'John Doe', 'age' => 17], 'name');// true

        2. 判断数组中是否存在指定的一个或多个键

$array = ['product' => ['name' => 'Desk', 'price' => 100]]; 

$contains = Arr::has($array, 'product.name');// true 

$contains = Arr::has($array, ['product.price', 'product.discount']);// false

        3.根据指定键查询数组,返回数组的值

$array = ['products' => ['desk' => ['price' => 100]]];

$price = Arr::get($array, 'products.desk.price');// 100

//函数也可以接受一个默认值,如果没有找到特定的键,将返回默认值
$discount = Arr::get($array, 'products.desk.discount', 0); //0

         4.返回数组中满足指定条件的第一个/最后一个元素

$array = [100, 200, 300];

$first = Arr::first($array, function ($value, $key) {
    return $value >= 150;
});// 200

//将默认值作为第三个参数传递给该方法,如果没有值满足条件,则返回该默认值:
$first = Arr::first($array, $callback, $default);


//函数返回数组中满足指定条件的最后一个元素用 last 
$last = Arr::last($array, function ($value, $key) {
    return $value >= 150;
}); //300

        5.根据条件查询数组 

$array = [100, '200', 300, '400', 500];

$filtered = Arr::where($array, function ($value, $key) {
    return is_string($value);
}); 
// [1 => '200', 3 => '400']

         6.根据指定的键查询数组,返回键值对(数组)

$array = ['name' => 'Desk', 'price' => 100, 'orders' => 10];

$slice = Arr::only($array, ['name', 'price']); 
// ['name' => 'Desk', 'price' => 100]

         7.根据指定的键,返回所有值(数组)

array = [
['developer' => ['id' => 1, 'name' => 'Taylor']],
['developer' => ['id' => 2, 'name' => 'Abigail']],
];

$names = Arr::pluck($array, 'developer.name'); 
// ['Taylor', 'Abigail']

##可以指定结果的键
$names = Arr::pluck($array, 'developer.name', 'developer.id'); 
// [1 => 'Taylor', 2 => 'Abigail']

         8.从数组中随机返回一个或多个值

$array = [1, 2, 3, 4, 5];

$random = Arr::random($array);
//4

$items = Arr::random($array, 2);
// [2, 5]  返回数组

 六、数组操作

        1.数组合并

Arr::collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
//[1, 2, 3, 4, 5, 6, 7, 8, 9]

        2. 返回多个数组具有所有可能排列的笛卡尔乘积

Arr::crossJoin([1, 2], ['a', 'b']);
// [[1, 'a'],[1, 'b'],[2, 'a'],[2, 'b']]

        3.将多维数组中所有的 键 平铺到一维数组中,新数组使用「.」符号表示层级包含关系

Arr::dot(['products' => ['desk' => ['price' => 100]]]); 
// ['products.desk.price' => 100]

        4.将多维数组中数组的 值 取出平铺为一维数组 

Arr::flatten(['name' => 'Joe', 'languages' => ['PHP', 'Ruby']]); 
// ['Joe', 'PHP', 'Ruby']

        5.使用「点表示法」的一维数组扩展为多维数组 

$array = [
    'user.name' => 'Kevin Malone',
    'user.occupation' => 'Accountant',
];

$array = Arr::undot($array);

// ['user' => ['name' => 'Kevin Malone', 'occupation' => 'Accountant']]

七、排序 

        1.随机排序

$array = Arr::shuffle([1, 2, 3, 4, 5]);
// [3, 2, 5, 1, 4] 

        2. 根据数组的值大小进行排序

$array = ['Desk', 'Table', 'Chair'];
$sorted = Arr::sort($array);
// ['Chair', 'Desk', 'Table']

//根据给定回调函数返回的结果对数组进行排序
$array = [
    ['name' => 'Desk'],
    ['name' => 'Table'],
    ['name' => 'Chair'],
];

$sorted = array_values(Arr::sort($array, function ($value) {
    return $value['name'];
}));

/*
    [
        ['name' => 'Chair'],
        ['name' => 'Desk'],
        ['name' => 'Table'],
    ]
*/

        3.递归排序 

$array = [
    ['Roman', 'Taylor', 'Li'],
    ['PHP', 'Ruby', 'JavaScript'],
    ['one' => 1, 'two' => 2, 'three' => 3],
];

$sorted = Arr::sortRecursive($array);

/*
    [
        ['JavaScript', 'PHP', 'Ruby'],
        ['one' => 1, 'three' => 3, 'two' => 2],
        ['Li', 'Roman', 'Taylor'],
    ]
*/

八、其他 

        1.用于动态css赋值,根据给定的条件编译并返回 CSS 类字符串,为true 的返回

$isActive = false;
$hasError = true;
$array = ['p-4', 'font-bold' => $isActive, 'bg-red' => $hasError];
$classes = Arr::toCssClasses($array);
//'p-4 bg-red'

九、数组函数

        1.数组修改,给多维数组或对象设置缺少的值

data = ['products' => ['desk' => ['price' => 100]]];

data_fill($data, 'products.desk.price', 200);//未成功修改,值存在 ['products' => ['desk' => ['price' => 100]]]

data_fill($data, 'products.desk.discount', 10);
// ['products' => ['desk' => ['price' => 100, 'discount' => 10]]]

//此函数也可以接收「*」作为通配符,设置相应缺少的值
$data = [
    'products' => [
        ['name' => 'Desk 1', 'price' => 100],
        ['name' => 'Desk 2'],
    ],
];
data_fill($data, 'products.*.price', 200);
/*
    [
        'products' => [
            ['name' => 'Desk 1', 'price' => 100],
            ['name' => 'Desk 2', 'price' => 200],
        ],
    ]
*/

        2. 数组修改,给多维数组或对象设置值

$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],
        ],
    ]
*/

        3.根据指定的键查询数组的值,返回String

$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'];

         4.返回数组中的第一个/最后一个值

$array = [100, 200, 300];
$first = head($array);// 100

//last 函数将返回数组中的最后一个值
$array = [100, 200, 300];
$last = last($array);// 300


 

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_37131747

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值