感触life

laravel辅助函数-数组

array_add()

array_add 函数添加给定键值对到数组 —— 如果给定键不存在的话:

$array = array_add(['name' => 'Desk'], 'price', 100);
// ['name' => 'Desk', 'price' => 100]

array_collapse()

array_collapse 函数将多个数组合并成一个:

$array = array_collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
// [1, 2, 3, 4, 5, 6, 7, 8, 9]

array_divide()

array_divide 函数返回两个数组,一个包含原数组的所有键,另外一个包含原数组的所有值:

list($keys, $values) = array_divide(['name' => 'Desk']);
// $keys: ['name']
// $values: ['Desk']

array_dot()

array_dot 函数使用”.”号将将多维数组转化为一维数组:

$array = ['products' => ['desk' => ['price' => 100]]];
$flattened = array_dot($array);
// ['products.desk.price' => 100]

array_except()

array_except 函数从数组中移除给定键值对:

$array = ['name' => 'Desk', 'price' => 100];
$array = array_except($array, ['price']);
// ['name' => 'Desk']

array_first()

array_first 函数返回通过测试数组的第一个元素:

$array = [100, 200, 300];

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

默认值可以作为第三个参数传递给该方法,如果没有值通过测试的话返回默认值:

$value = array_first($array, $callback, $default);

array_flatten()

array_flatten 函数将多维数组转化为一维数组:

$array = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']];

$array = array_flatten($array);

// ['Joe', 'PHP', 'Ruby'];

array_forget()

array_forget 函数使用”.”号从嵌套数组中移除给定键值对:

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

array_forget($array, 'products.desk');

// ['products' => []]

array_get()

array_get 方法使用”.”号从嵌套数组中获取值:

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

$value = array_get($array, 'products.desk.price');

// ['price' => 100]

array_get 函数还接收一个默认值,如果指定键不存在的话则返回该默认值:

$value = array_get($array, 'products.desk.discount', 0);
// 0

array_has()

array_has 函数使用“.”检查给定数据项是否在数组中存在:

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

$hasItem = array_has($array, 'product.name');

// true

$hasItems = array_has($array, ['product.price', 'product.discount']);

// false

array_last()

array_last 函数返回通过过滤数组的最后一个元素:

$array = [100, 200, 300, 110];

$value = array_last($array, function ($value, $key) {
    return $value >= 150;
});

// 300

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

$last = array_last($array, $callback, $default);

array_only()

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

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

$array = array_only($array, ['name', 'price']);

// ['name' => 'Desk', 'price' => 100]

array_pluck()

array_pluck 方法从数组中返回给定键对应的键值对列表:

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

$names = array_pluck($array, 'developer.name');

// ['Taylor', 'Abigail']

你还可以指定返回结果的键:

$array = array_pluck($array, 'developer.name', 'developer.id');

// [1 => 'Taylor', 2 => 'Abigail'];

array_prepend()

array_prepend 函数将数据项推入数组开头:

$array = ['one', 'two', 'three', 'four'];

$array = array_prepend($array, 'zero');

// $array: ['zero', 'one', 'two', 'three', 'four']

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

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

$array = array_prepend($array, 'Desk', 'name');

// ['name' => 'Desk', 'price' => 100]

array_pull()

array_pull 函数从数组中返回并移除键值对:

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

$name = array_pull($array, 'name');

// $name: Desk

// $array: ['price' => 100]

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

$value = array_pull($array, $key, $default);

array_random()

array_random 函数从数组中返回随机值:

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

$random = array_random($array);

// 4 - (retrieved randomly)

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

$items = array_random($array, 2);

// [2, 5] - (retrieved randomly)

array_set()

array_set 函数用于在嵌套数组中使用”.”号设置值:

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

array_set($array, 'products.desk.price', 200);

// ['products' => ['desk' => ['price' => 200]]]

array_sort()

array_sort 函数通过值对数组进行排序:

$array = ['Desk', 'Table', 'Chair'];

$sorted = array_sort($array);

// ['Chair', 'Desk', 'Table']

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

$array = [
    ['name' => 'Desk'],
    ['name' => 'Table'],
    ['name' => 'Chair'],
];

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

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

array_sort_recursive()

array_sort_recursive 函数使用 sort 函数对数组进行递归排序:

$array = [
    ['Roman', 'Taylor', 'Li'],
    ['PHP', 'Ruby', 'JavaScript'],
];

$array = array_sort_recursive($array);

/*
    [
        ['Li', 'Roman', 'Taylor'],
        ['JavaScript', 'PHP', 'Ruby'],
    ]
*/

array_where()

array_where 函数使用给定闭包对数组进行过滤:

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

$array = array_where($array, function ($value, $key) {
    return is_string($value);
});

// [1 => 200, 3 => 400]

array_wrap()

array_wrap 函数将给定值包裹到数组中,如果给定值已经是数组则保持不变:

$string = 'Laravel';

$array = array_wrap($string);

// ['Laravel']

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

$nothing = null;

$array = array_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_set()

data_set 函数使用 「.」号设置嵌套数组或对象的值:

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

data_set($data, 'products.desk.price', 200);

// ['products' => ['desk' => ['price' => 200]]]

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

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

$array = [100, 200, 300];

$first = head($array);
// 100

last()

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

$array = [100, 200, 300];

$last = last($array);

// 300

码字很辛苦,转载请注明来自感触life-博客《laravel辅助函数-数组》

评论