辅助函数 数组和对象

Laravel 辅助函数 数组和对象

返回 Laravel 辅助函数


Arr::accessible()

Arr::accessible() 方法会检查给定值是否可以通过数组方式访问:

use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
​
$isAccessible = Arr::accessible(['a' => 1, 'b' => 2]);
​
// true
​
$isAccessible = Arr::accessible(new Collection);
​
// true
​
$isAccessible = Arr::accessible('abc');
​
// false
​
$isAccessible = Arr::accessible(new stdClass);
​
// false

Arr::add()

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]

Arr::collapse()

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]

Arr::crossJoin

Arr::crossJoin 方法可以交叉连接给定数组,返回一个包含所有排列组合的笛卡尔乘积:

use Illuminate\Support\Arr;
​
$matrix = Arr::crossJoin([1, 2], ['a', 'b']);
​
/*
    [
        [1, 'a'],
        [1, 'b'],
        [2, 'a'],
        [2, 'b'],
    ]
*/
​
$matrix = Arr::crossJoin([1, 2], ['a', 'b'], ['I', 'II']);
​
/*
    [
        [1, 'a', 'I'],
        [1, 'a', 'II'],
        [1, 'b', 'I'],
        [1, 'b', 'II'],
        [2, 'a', 'I'],
        [2, 'a', 'II'],
        [2, 'b', 'I'],
        [2, 'b', 'II'],
    ]
*/

Arr::divide()

Arr::divide 方法返回两个数组,一个包含原数组的所有键,另外一个包含原数组的所有值:

use Illuminate\Support\Arr;
​
[$keys, $values] = Arr::divide(['name' => 'Desk']);
​
// $keys: ['name']
​
// $values: ['Desk']

Arr::dot()

Arr::dot 方法使用「.」号将将多维数组转化为一维数组:

use Illuminate\Support\Arr;
​
$array = ['products' => ['desk' => ['price' => 100]]];
​
$flattened = Arr::dot($array);
​
// ['products.desk.price' => 100]

Arr::except()

Arr::except 方法从数组中移除给定键值对:

use Illuminate\Support\Arr;
​
$array = ['name' => 'Desk', 'price' => 100];
​
$filtered = Arr::except($array, ['price']);
​
// ['name' => 'Desk']

Arr::exists()

Arr::exists() 方法检查给定键在数组中是否存在:

use Illuminate\Support\Arr;
​
$array = ['name' => 'John Doe', 'age' => 17];
​
$exists = Arr::exists($array, 'name');
​
// true
​
$exists = Arr::exists($array, 'salary');
​
// false

Arr::first()

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

Arr::flatten()

Arr::flatten 方法将多维数组转化为一维数组:

use Illuminate\Support\Arr;
​
$array = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']];
​
$flattened = Arr::flatten($array);
​
// ['Joe', 'PHP', 'Ruby']

Arr::forget()

Arr::forget 方法使用「.」号从嵌套数组中移除给定键值对:

use Illuminate\Support\Arr;
​
$array = ['products' => ['desk' => ['price' => 100]]];
​
Arr::forget($array, 'products.desk');
​
// ['products' => []]

Arr::get()

Arr::get 方法使用「.」号从嵌套数组中获取值:

use Illuminate\Support\Arr;
​
$array = ['products' => ['desk' => ['price' => 100]]];
​
$price = Arr::get($array, 'products.desk.price');
​
// 100

Arr::get 方法还接收一个默认值,如果指定键不存在的话则返回该默认值:

use Illuminate\Support\Arr;
​
$discount = Arr::get($array, 'products.desk.discount', 0);
​
// 0

Arr::has()

Arr::has 方法使用「.」检查给定数据项是否在数组中存在:

use Illuminate\Support\Arr;
​
$array = ['product' => ['name' => 'Desk', 'price' => 100]];
​
$contains = Arr::has($array, 'product.name');
​
// true
​
$contains = Arr::has($array, ['product.price', 'product.discount']);
​
// false

Arr::hasAny

Arr::hasAny 方法检查给定集合中的任意项(通过 . 访问)是否在数组中存在:

use Illuminate\Support\Arr;
​
$array = ['product' => ['name' => 'Desk', 'price' => 100]];
​
$contains = Arr::hasAny($array, 'product.name');
​
// true
​
$contains = Arr::hasAny($array, ['product.name', 'product.discount']);
​
// true
​
$contains = Arr::hasAny($array, ['category', 'product.discount']);
​
// false

Arr::isAssoc()

Arr::isAssoc 方法会在给定数组是关联数组时返回 true,如果一个数组没有包含从0开始的数字序列键,就被认为是「关联数组」:

use Illuminate\Support\Arr;
​
$isAssoc = Arr::isAssoc(['product' => ['name' => 'Desk', 'price' => 100]]);
​
// true
​
$isAssoc = Arr::isAssoc([1, 2, 3]);
​
// false

Arr::last()

Arr::last 方法返回通过过滤数组的最后一个元素:

use Illuminate\Support\Arr;
​
$array = [100, 200, 300, 110];
​
$last = Arr::last($array, function ($value, $key) {
    return $value >= 150;
});
​
// 300

我们可以传递一个默认值作为第三个参数到该方法,如果没有值通过真理测试的话该默认值被返回:

use Illuminate\Support\Arr;
​
$last = Arr::last($array, $callback, $default);
Arr::only()

Arr::only 方法只从给定数组中返回指定键值对:

use Illuminate\Support\Arr;
​
$array = ['name' => 'Desk', 'price' => 100, 'orders' => 10];
​
$slice = Arr::only($array, ['name', 'price']);
​
// ['name' => 'Desk', 'price' => 100]

Arr::pluck()

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

Arr::prepend()

Arr::prepend 方法将数据项推入数组开头:

use Illuminate\Support\Arr;
​
$array = ['one', 'two', 'three', 'four'];
​
$array = Arr::prepend($array, 'zero');

// ['zero', 'one', 'two', 'three', 'four']

如果需要的话还可以指定用于该值的键:

use Illuminate\Support\Arr;
​
$array = ['price' => 100];
​
$array = Arr::prepend($array, 'Desk', 'name');
​
// ['name' => 'Desk', 'price' => 100]

Arr::pull()

Arr::pull 方法从数组中返回并移除键值对:

use Illuminate\Support\Arr;
​
$array = ['name' => 'Desk', 'price' => 100];
​
$name = Arr::pull($array, 'name');
​
// $name: Desk
​
// $array: ['price' => 100]

我们还可以传递默认值作为第三个参数到该方法,如果指定键不存在的话返回该值:

use Illuminate\Support\Arr;
​
$value = Arr::pull($array, $key, $default);

Arr::query()

Arr::query 方法会转化数组为查询字符串:

use Illuminate\Support\Arr;
​
$array = ['name' => 'Taylor', 'order' => ['column' => 'created_at', 'direction' => 'desc']];
​
Arr::query($array);
​
// name=Taylor&order[column]=created_at&order[direction]=desc

Arr::random()

Arr::random 方法从数组中返回随机值:

use Illuminate\Support\Arr;
​
$array = [1, 2, 3, 4, 5];
​
$random = Arr::random($array);
​
// 4 - (retrieved randomly)

还可以指定返回的数据项数目作为可选的第二个参数,需要注意的是提供这个参数会返回一个数组,即使只返回一个数据项:

use Illuminate\Support\Arr;
​
$items = Arr::random($array, 2);
​
// [2, 5] - (retrieved randomly)

Arr::set()

Arr::set 方法用于在嵌套数组中使用「.」号设置值:

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

Arr::shuffle()

Arr::shuffle 方法会随机打乱数组中元素的排序:

use Illuminate\Support\Arr;
​
$array = Arr::shuffle([1, 2, 3, 4, 5]);
​
// [3, 2, 5, 1, 4] - (generated randomly)

Arr::sort()

Arr::sort 方法通过值对数组进行排序:

use Illuminate\Support\Arr;
​
$array = ['Desk', 'Table', 'Chair'];
​
$sorted = Arr::sort($array);
​
// ['Chair', 'Desk', 'Table']

还可以通过给定闭包的结果对数组进行排序:

use Illuminate\Support\Arr;
​
$array = [
    ['name' => 'Desk'],
    ['name' => 'Table'],
    ['name' => 'Chair'],
];
​
$sorted = array_values(Arr::sort($array, function ($value) {
    return $value['name'];
}));
​
/*
    [
        ['name' => 'Chair'],
        ['name' => 'Desk'],
        ['name' => 'Table'],
    ]
*/

Arr::sortRecursive()

Arr::sortRecursive 方法使用 sort 函数对索引数组、ksort 函数对关联数组进行递归排序:

use Illuminate\Support\Arr;
​
$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'],
    ]
*/

Arr::where()

Arr::where 方法使用给定闭包对数组进行过滤:

use Illuminate\Support\Arr;
​
$array = [100, '200', 300, '400', 500];
​
$filtered = Arr::where($array, function ($value, $key) {
    return is_string($value);
});
​
// [1 => '200', 3 => '400']

Arr::wrap()

Arr::wrap 方法将给定值包裹到数组中,如果给定值已经是数组则保持不变:

use Illuminate\Support\Arr;
​
$string = 'Laravel';
​
$array = Arr::wrap($string);
​
// ['Laravel']

如果给定值是空的,则返回一个空数组:

use Illuminate\Support\Arr;
$nothing = null;
$array = Arr::wrap($nothing);
// []

data_fill()

data_fill 函数使用「.」号以嵌套数组或对象的方式设置缺失值:

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

data_get()

data_get 函数使用「.」号从嵌套数组或对象中获取值:

$data = ['products' => ['desk' => ['price' => 100]]];
​
$price = data_get($data, 'products.desk.price');
​
// 100

data_get 函数还接收默认值,以便指定键不存在的情况下返回:

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

data_set()

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]]]

head 函数只是简单返回给定数组的第一个元素:

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

last()

last 函数返回给定数组的最后一个元素:

$array = [100, 200, 300];
​
$last = last($array);
​
// 300

返回 Laravel 辅助函数

查看笔记

扫码一下
查看教程更方便