chr15k / 数组
出色的数组助手包
Requires
- php: ^7.4
Requires (Dev)
- nesbot/carbon: ^2.36
- phpunit/phpunit: ^9.2
This package is auto-updated.
Last update: 2024-09-16 07:38:29 UTC
README
此包为使用PHP操作数组提供了有用的助手。
基于 ...
- Laravel的数组助手工作 (https://github.com/laravel/framework)
安装
您可以通过composer安装此包
composer require chr15k/array
用法
- 可访问的
- 添加
- 折叠
- 交叉连接
- 分割
- 点操作
- 排除
- 存在
- 第一个
- 扁平化
- 忘记
- 获取
- 有
- 有任何
- 是否关联
- 是否是多维的
- 最后的
- 仅有的
- 提取
- 添加到前面
- 拉取
- 查询
- 随机
- 设置
- 洗牌
- 排序
- 递归排序
- 哪里
- 包装
Arr::accessible()
Arr::accessible方法检查给定值是否为数组可访问
<?php use Chr15k\Arr\Arr; $isAccessible = Arr::accessible(['a' => 1, 'b' => 2]); // true $isAccessible = Arr::accessible('abc'); // false $isAccessible = Arr::accessible(new stdClass); // false
Arr::add()
Arr::add方法在给定键/值对不存在于数组中或被设置为null时,将其添加到数组中
<?php use Chr15k\Arr\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方法将数组数组折叠成一个单一数组
<?php use Chr15k\Arr\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方法交叉连接给定数组,返回所有可能的排列的笛卡尔积
<?php use Chr15k\Arr\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方法返回两个数组,一个包含数组的键,另一个包含值
<?php use Chr15k\Arr\Arr; [$keys, $values] = Arr::divide(['name' => 'Desk']); // $keys: ['name'] // $values: ['Desk']
Arr::dot()
Arr::dot方法将多维数组扁平化为单级数组,使用“点”表示深度
<?php use Chr15k\Arr\Arr; $array = ['products' => ['desk' => ['price' => 100]]]; $flattened = Arr::dot($array); // ['products.desk.price' => 100]
Arr::except()
Arr::except方法从数组中删除给定的键/值对
<?php use Chr15k\Arr\Arr; $array = ['name' => 'Desk', 'price' => 100]; $filtered = Arr::except($array, ['price']); // ['name' => 'Desk']
Arr::exists()
Arr::exists方法检查给定的键是否存在于提供的数组中
<?php use Chr15k\Arr\Arr; $array = ['name' => 'John Doe', 'age' => 17]; $exists = Arr::exists($array, 'name'); // true $exists = Arr::exists($array, 'salary'); // false
Arr::first()
Arr::first方法返回通过给定真值测试的数组的第一个元素
<?php use Chr15k\Arr\Arr; $array = [100, 200, 300]; $first = Arr::first($array, function ($value, $key) { return $value >= 150; }); // 200 // A default value may also be passed as the third parameter to the method. This value will be returned if no value passes the truth test: $first = Arr::first($array, $callback, $default);
Arr::flatten()
Arr::flatten方法将多维数组扁平化为单级数组
<?php use Chr15k\Arr\Arr; $array = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']]; $flattened = Arr::flatten($array); // ['Joe', 'PHP', 'Ruby']
Arr::forget()
Arr::forget方法使用“点”表示法从深度嵌套数组中删除给定的键/值对
<?php use Chr15k\Arr\Arr; $array = ['products' => ['desk' => ['price' => 100]]]; Arr::forget($array, 'products.desk'); // ['products' => []]
Arr::get()
Arr::get方法使用“点”表示法从深度嵌套数组中检索值
<?php use Chr15k\Arr\Arr; $array = ['products' => ['desk' => ['price' => 100]]]; $price = Arr::get($array, 'products.desk.price'); // 100 // The Arr::get method also accepts a default value, which will be returned if the specific key is not found: $discount = Arr::get($array, 'products.desk.discount', 0); // 0
Arr::has()
Arr::has方法使用“点”表示法检查给定项或多项是否存在于数组中
<?php use Chr15k\Arr\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方法检查给定集合中的任何项是否存在于数组中,使用“点”表示法
<?php use Chr15k\Arr\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 表示给定的数组是一个关联数组。一个数组被认为是“关联的”,如果它没有以零开始的顺序数字键
<?php use Chr15k\Arr\Arr; $isAssoc = Arr::isAssoc(['product' => ['name' => 'Desk', 'price' => 100]]); // true $isAssoc = Arr::isAssoc([1, 2, 3]); // false
Arr::isMultiDimensional()
Arr::isMultiDimensional 方法返回 true 表示给定的数组是多维的
<?php use Chr15k\Arr\Arr; $isMultiDimensional = Arr::isMultiDimensional(['product' => ['name' => 'Desk', 'price' => 100]]); // true $isMultiDimensional = Arr::isMultiDimensional([2, 3, [4]]); // true $isMultiDimensional = Arr::isMultiDimensional([2, 3, 4]); // false $isMultiDimensional = Arr::isMultiDimensional(['name' => 'Desk', 'price' => 100]); // false // Accounts for empty arrays $isMultiDimensional = Arr::isMultiDimensional(['name' => 'Desk', 'price' => 100, []]); // true
Arr::last()
Arr::last 方法返回数组通过给定真值测试的最后一个元素
<?php use Chr15k\Arr\Arr; $array = [100, 200, 300, 110]; $last = Arr::last($array, function ($value, $key) { return $value >= 150; }); // 300 // A default value may be passed as the third argument to the method. This value will be returned if no value passes the truth test: $last = Arr::last($array, $callback, $default);
Arr::only()
Arr::only 方法返回给定数组中指定的键/值对
<?php use Chr15k\Arr\Arr; $array = ['name' => 'Desk', 'price' => 100, 'orders' => 10]; $slice = Arr::only($array, ['name', 'price']); // ['name' => 'Desk', 'price' => 100]
Arr::pluck()
Arr::pluck 方法从数组中检索给定键的所有值
<?php use Chr15k\Arr\Arr; $array = [ ['developer' => ['id' => 1, 'name' => 'Taylor']], ['developer' => ['id' => 2, 'name' => 'Abigail']], ]; $names = Arr::pluck($array, 'developer.name'); // ['Taylor', 'Abigail'] // You may also specify how you wish the resulting list to be keyed: $names = Arr::pluck($array, 'developer.name', 'developer.id'); // [1 => 'Taylor', 2 => 'Abigail']
Arr::prepend()
Arr::prepend 方法将一个项推送到数组的开始位置
<?php use Chr15k\Arr\Arr; $array = ['one', 'two', 'three', 'four']; $array = Arr::prepend($array, 'zero'); // ['zero', 'one', 'two', 'three', 'four'] // If needed, you may specify the key that should be used for the value: $array = ['price' => 100]; $array = Arr::prepend($array, 'Desk', 'name'); // ['name' => 'Desk', 'price' => 100]
Arr::pull()
Arr::pull 方法从数组中返回并移除一个键/值对
<?php use Chr15k\Arr\Arr; $array = ['name' => 'Desk', 'price' => 100]; $name = Arr::pull($array, 'name'); // $name: Desk // $array: ['price' => 100] // A default value may be passed as the third argument to the method. This value will be returned if the key doesn't exist: $value = Arr::pull($array, $key, $default);
Arr::query()
Arr::query 方法将数组转换为查询字符串
<?php use Chr15k\Arr\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 方法从数组中返回一个随机值
<?php use Chr15k\Arr\Arr; $array = [1, 2, 3, 4, 5]; $random = Arr::random($array); // 4 - (retrieved randomly) // You may also specify the number of items to return as an optional second argument. // Note that providing this argument will return an array, even if only one item is desired: $items = Arr::random($array, 2); // [2, 5] - (retrieved randomly)
Arr::set()
Arr::set 方法使用“点”表示法在深度嵌套数组中设置值
<?php use Chr15k\Arr\Arr; $array = ['products' => ['desk' => ['price' => 100]]]; Arr::set($array, 'products.desk.price', 200); // ['products' => ['desk' => ['price' => 200]]]
Arr::shuffle()
Arr::shuffle 方法随机打乱数组中的项
<?php use Chr15k\Arr\Arr; $array = Arr::shuffle([1, 2, 3, 4, 5]); // [3, 2, 5, 1, 4] - (generated randomly)
Arr::sort()
Arr::sort 方法按值对数组进行排序
<?php use Chr15k\Arr\Arr; $array = ['Desk', 'Table', 'Chair']; $sorted = Arr::sort($array); // ['Chair', 'Desk', 'Table'] // Reverse the order by passing true to the second argument $sorted = Arr::sort($array, true); // ['Table', 'Desk', 'Chair']
Arr::sortRecursive()
Arr::sortRecursive 方法递归地对数组进行排序,使用排序函数对数字子数组进行排序,使用 ksort 对关联子数组进行排序
<?php use Chr15k\Arr\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 方法使用给定的闭包过滤数组
<?php use Chr15k\Arr\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 方法将给定的值包裹在数组中。如果给定的值已经是数组,则不会更改它
<?php use Chr15k\Arr\Arr; $string = 'Laravel'; $array = Arr::wrap($string); // ['Laravel'] // If the given value is null, an empty array will be returned: $nothing = null; $array = Arr::wrap($nothing); // []
测试
您可以使用以下命令运行测试
vendor/bin/phpunit
许可证
MIT 许可证(MIT)。请参阅 许可证文件 以获取更多信息。