barogue/collection

PHP 原生数组的包装类

0.0.2 2023-08-27 12:09 UTC

This package is auto-updated.

Last update: 2024-08-28 16:01:40 UTC


README

Tests codecov Licence Badge Release Badge Tag Badge Issues Badge Code Size

PHP 原生数组的包装类

兼容性和依赖关系

此库与 PHP 版本 8.18.2 兼容。

此库没有依赖项。

安装

使用 composer 简单安装。

composer require barogue/collections

或者,直接将其添加到您的 composer.json 文件中

{
    "require": {
        "barogue/collections": "^1.0"
    }
}

贡献

此库遵循 PSR-1PSR-2 标准。

单元测试

在推送任何更改之前,请确保所有单元测试都通过。

如果可能,请在单独的提交中提高覆盖率。

vendor/bin/phpunit

代码检查器

在推送之前,请确保您已运行代码检查器。 仅使用最低支持的 PHP 版本(8.1)运行

vendor/bin/php-cs-fixer fix

静态分析

在推送之前,请确保您已运行静态分析工具。

vendor/bin/phan

基准测试

在推送之前,请确保您已检查基准测试并确保您的代码没有引入任何减速。

在单独的提交中,您可以自由地加快现有代码的速度。

在单独的提交中,您可以自由地添加更多基准测试以获得更全面的覆盖。

vendor/bin/phpbench run --report=speed
vendor/bin/phpbench run --report=speed --output=markdown
vendor/bin/phpbench run --report=speed --filter=benchNetFromTax --iterations=50 --revs=50000

vendor/bin/phpbench xdebug:profile
vendor/bin/phpbench xdebug:profile --gui

文档

此库添加了一个新类,可以包装原生数组,以便更快、更简单地与之交互。

以下是可以找到新功能文档的链接。

创建 collection 实例

use Barogue\Collections\Collection;

// Using the constructor
$collection = new Collection();
$collection = new Collection([1, 2, 3]);

// Using the chainable constructor
$collection = Collection::instance();
$collection = Collection::instance([1, 2, 3]);

// Create using a range
$celsius = Collection::range(0, 100);
$alphabet = Collection::range('a', 'z');
$evens = Collection::range(0, 100, 2);

从 collection 获取数据

use Barogue\Collections\Collection;

// Collections can be used exactly like a normal array
$collection = new Collection([1, 2, 3]);
$collection[] = 4;
$collection['test'] = 5;
echo $collection[1]; // 2 

// Get the original array
$collection->getArray();

// Get an iterator
$collection->getIterator();

// Getting the size of the collection
echo count($collection); // 5
echo $collection->count(); // 5

集成到此类中的原生方法列表

array_change_key_case 改变数组中所有键的大小写

use Barogue\Collections\Collection;

$collection = new Collection(["FirSt" => 1, "SecOnd" => 4]);

$collection->changeKeyCase(CASE_UPPER); // ["FIRST" => 1, "SECOND" => 4]
$collection->changeKeyCase(CASE_LOWER); // ["first" => 1, "second" => 4]

$collection->changeKeyUpperCase(); // ["FIRST" => 1, "SECOND" => 4]
$collection->changeKeyLowerCase(); // ["first" => 1, "second" => 4]

array_chunk 将数组分割成块

use Barogue\Collections\Collection;

$collection = Collection::range(1, 100);
$chunks = $collection->chunk(10);

array_column 从输入数组中返回单个列的值

use Barogue\Collections\Collection;

$collection = new Collection([
    'player_1' => [
        'name' => 'John',
        'stats' => [
            'hp' => 50,
            'exp' => 1000
        ]
    ],
    'player_2' => [
        'name' => 'Jane',
        'stats' => [
            'hp' => 70,
            'exp' => 1000
        ]
    ]
]);
$hps = $collection->column('stats.hp'); // [50, 70]
$hps = $collection->column('stats.hp', 'name'); // ['John' => 50, 'Jane' => 70]

array_combine 使用一个数组作为键,另一个数组作为其值创建数组

use Barogue\Collections\Collection;

$keys = new Collection(['a', 'b', 'c']);
$combined = $keys->combine(1, 2, 3); // ['a' => 1, 'b' => 2, 'c' => 3]


$combined = Collection::instance(['a', 'b', 'c'])->combine(1, 2, 3); // ['a' => 1, 'b' => 2, 'c' => 3]

array_count_values 统计数组中的所有值

use Barogue\Collections\Collection;

$collection = new Collection([1, 2, 3, 1, 2, 4, 'a', 'a', 1]);

$appearances = $collection->countValues(); // [1 => 3, 2 => 2, 3 => 1, 4 => 1, 'a' => 2]

array_diff_assoc 使用附加索引检查计算数组的差异

// Add documentation

array_diff_key 使用键进行比较计算数组的差异

// Add documentation

array_diff_uassoc 使用用户提供的回调函数进行附加索引检查,计算数组的差异

// Add documentation

array_diff_ukey 使用回调函数对键进行比较,计算数组的差异

// Add documentation

array_diff 计算数组的差异

use Barogue\Collections\Collection;

$collection = new Collection([1, 2, 3, 4, 5, 6]);
$diff = $collection->diff([3, 4, 5]);
$diff = $collection->diff(new Collection([3, 4, 5]));

array_fill_keys 使用键指定值填充数组

// Add documentation

array_fill 使用值填充数组

// Add documentation

array_filter 使用回调函数过滤数组元素

use Barogue\Collections\Collection;

$collection = new Collection([1, 2, 3, 4, 5, 6, null]);

$collection->filter();
$collection->filter(function ($item) {
    return $item > 3;
});

array_flip 交换数组中的所有键与对应的值

use Barogue\Collections\Collection;

$collection = new Collection(['a', 'b', 'c']);
$flipped = $collection->flip(); // ['a' => 0, 'b' => 1, 'c' => 2]

$collection = new Collection(['a', 'b', 'c', 'a']);
$flipped = $collection->flip(); // ['a' => 0, 'b' => 1, 'c' => 2]
$doubleFlipped = $collection->flip()->flip(); // ['a' => 0, 'b' => 1, 'c' => 2]

array_intersect_assoc 计算数组的交集,并附加索引检查

// Add documentation

array_intersect_key 使用键进行比较以计算数组的交集

// Add documentation

array_intersect_uassoc 计算数组的交集,附加索引检查,通过回调函数比较索引

// Add documentation

array_intersect_ukey 使用回调函数在键上比较以计算数组的交集

// Add documentation

array_intersect 计算数组的交集

// Add documentation

array_is_list 检查给定的数组是否是列表

use Barogue\Collections\Collection;

Collection::instance(['a', 'b', 'c'])->isList(); // true
Collection::instance(['a' => 1, 'b', 'c'])->isList(); // false

array_key_exists 检查给定的键或索引是否存在于数组中

use Barogue\Collections\Collection;

$collection = new Collection([
    'a' => 1,
    'b' => 2,
    'c' => [
        'a' => 1,
        'b' => null,
        'c' => 3,
    ],
]);
$collection->exists('a'); // true
$collection->exists('z'); // false
$collection->exists('c.a'); // true
$collection->exists('c.b'); // true
$collection->exists('c.z'); // false

array_key_first 获取数组的第一个键

use Barogue\Collections\Collection;

$collection = new Collection([
    'a' => 1,
    'b' => 2,
    'c' => 3,
    'd' => 4,
    'e' => 5,
]);

$collection->firstKey(); // 'a'
$collection->firstKey(fn($value, $key) => $value >= 3); // 'c'
$collection->firstKey(fn($value, $key) => $key != 'a'); // 'b'

array_key_last 获取数组的最后一个键

use Barogue\Collections\Collection;

$collection = new Collection([
    'a' => 1,
    'b' => 2,
    'c' => 3,
    'd' => 4,
    'e' => 5,
]);

$collection->lastKey(); // 'e'
$collection->lastKey(fn($value, $key) => $value >= 3); // 'e'
$collection->lastKey(fn($value, $key) => $key != 'a'); // 'e'

array_keys 返回数组的所有键或键的子集

use Barogue\Collections\Collection;

$collection = new Collection([
    'a' => 1,
    'b' => 2,
    'c' => [
        'a' => 1,
        'b' => null,
        'c' => 3,
    ],
]);

$collection->keys(); // ['a', 'b', 'c']

$collection->keys(true); // ['a', 'b', 'c.a', 'c.b', 'c.c']

array_map 将回调函数应用于给定的数组的元素

use Barogue\Collections\Collection;

$collection = new Collection([
    'a' => 1,
    'b' => 2,
    'c' => 3
]);

$increased = $collection->map(function ($value) {
    return $value * 2;
}); // ['a' => 2, 'b' => 4, 'c' => 6]

$concatenatedKeys = $collection->map(function ($value, $key) {
    return $key.'-'.$value;
}); // ['a' => 'a-1', 'b' => 'b-2', 'c' => 'c-3']

array_merge_recursive 递归地合并一个或多个数组

// Add documentation

array_merge 合并一个或多个数组

// Add documentation

array_multisort 对多个或多维数组进行排序

// Add documentation

array_pad 使用指定值填充数组到指定的长度

// Add documentation

array_pop 从数组的末尾弹出元素

use Barogue\Collections\Collection;

$collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8]);
$single = $collection->pop(); // 8
$multiple = $collection->pop(4)->getArray(); // [7, 6, 5, 4]

array_product 计算数组中值的乘积

// Add documentation

array_push 将一个或多个元素推送到数组的末尾

use Barogue\Collections\Collection;

$collection = new Collection();
$collection->push('test'); // ['test']
$collection->push(1, 2, 3, 4, 5, 6);  // ['test', 1, 2, 3, 4, 5]

array_rand 从数组中随机选择一个或多个键

use Barogue\Collections\Collection;

$collection = new Collection([
    'a' => 1,
    'b' => 2,
    'c' => 3
]);

$collection->randomKey(); // 'b'
$collection->randomKeys(2)->getArray(); // ['c', 'a']

array_reduce 使用回调函数迭代地减少数组到一个单一值

use Barogue\Collections\Collection;

$factorial = Collection::range(10, 1)->reduce(fn($carry, $value) => $carry * $value, 1)
$factorial = Collection::factorial(10)

array_replace_recursive 递归地将传入数组的元素替换到第一个数组中

// Add documentation

array_replace 将传入数组的元素替换到第一个数组中

// Add documentation

array_reverse 返回一个元素顺序相反的数组

use Barogue\Collections\Collection;

$collection = new Collection([
    'a' => 1,
    'b' => 2,
    'c' => 3
]);

$reversedCopy = $collection->reverse(); // ['c' => 3, 'b' => 2, 'a' => 1]

array_search 在数组中搜索指定的值,如果成功则返回第一个对应的键

// Add documentation

array_shift 从数组的开始处移除一个元素

use Barogue\Collections\Collection;

$collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8]);
$single = $collection->shift(); // 1
$multiple = $collection->shift(4)->getArray(); // [2, 3, 4, 5]

array_slice 从数组中提取一段

// Add documentation

array_splice 从数组中移除一部分,并用其他内容替换

// Add documentation

array_sum 计算数组中值的总和

use Barogue\Collections\Collection;

$sum = Collection::instance([1, 2, 3])->sum(); // 6
$sum = Collection::range(1, 100)->sum(); // 5050

array_udiff_assoc 计算数组的差异,带有额外的索引检查,通过回调函数比较数据

// Add documentation

array_udiff_uassoc 计算数组的差异,带有额外的索引检查,通过回调函数比较数据和索引

// Add documentation

array_udiff 通过回调函数比较数据来计算数组的差异

// Add documentation

array_uintersect_assoc 计算数组的交集,带有额外的索引检查,通过回调函数比较数据

// Add documentation

array_uintersect_uassoc 计算数组的交集,带有额外的索引检查,通过不同的回调函数比较数据和索引

// Add documentation

array_uintersect 计算数组的交集,通过回调函数比较数据

// Add documentation

array_unique 从数组中移除重复的值

// Add documentation

array_unshift 向数组的开始处添加一个或多个元素

// Add documentation

array_values 返回数组的所有值

use Barogue\Collections\Collection;

$values = Collection::instance(['a' => 1, 'b' => 2])->values()->getArray(); // [1, 2]

array_walk_recursive 递归地应用于数组的每个成员的函数

// Add documentation

array_walk 将用户提供的函数应用于数组的每个成员

// Add documentation

arsort 按降序对数组进行排序,并保持索引关联

use Barogue\Collections\Collection;

$collection = new Collection(['a' => 9, 'b' => 1, 'c' => 5]);
$collection->reverseSort(); // ['a' => 9, 'c' => 5, 'b' => 1]

asort 按升序对数组进行排序,并保持索引关联

use Barogue\Collections\Collection;

$collection = new Collection(['a' => 9, 'b' => 5, 'c' => 1]);
$collection->sort(); // ['c' => 1, 'b' => 5, 'a' => 9]

compact 创建包含变量及其值的数组

// Add documentation

count 计算数组中或可数对象中的所有元素

use Barogue\Collections\Collection;

$collection = new Collection([1, 2, 3]);

count($collection); // 3
$collection->count(); // 3

current 返回数组中的当前元素

// Add documentation

each 从数组中返回当前键值对,并前进数组光标

// Add documentation

extract 从数组导入变量到当前符号表

// Add documentation

implode 将数组元素连接成一个字符串

use Barogue\Collections\Collection;
echo Collection::instance('a', 'b', 'c')->implode(', '); // "a, b, c"
echo Collection::instance('a', 'b', 'c')->implode(', ', ' and '); // "a, b and c"

in_array 检查一个值是否存在于数组中

// Add documentation

krsort 按键降序排序数组

use Barogue\Collections\Collection;

$collection = new Collection(['a' => 5, 'c' => 4, 'z' => 3, 'b' => 2, 'e' => 1]);
$sorted = $collection->sortKeys()->reverse(); // ['z' => 3, 'e' => 1, 'c' => 4, 'b' => 2, 'a' => 5]

ksort 按键升序排序数组

use Barogue\Collections\Collection;

$collection = new Collection(['a' => 5, 'c' => 4, 'z' => 3, 'b' => 2, 'e' => 1]);
$sorted = $collection->sortKeys(); // ['a' => 5, 'b' => 2, 'c' => 4, 'e' => 1, 'z' => 3]

list 将变量分配为如果它们是一个数组

use Barogue\Collections\Collection;

$collection = new Collection(['coffee', 'brown', 'caffeine']);
list($drink, $color, $power) = $collection;
echo $drink; // coffee
echo $color; // brown
echo $power; // caffeine

natcasesort 使用不区分大小写的“自然顺序”算法对数组进行排序

// Add documentation

natsort 使用“自然顺序”算法对数组进行排序

// Add documentation

range 创建一个包含一系列元素的数组

use Barogue\Collections\Collection;

$numbers = Collection::range(0, 100);
$even = Collection::range(0, 100, 2);
$alphabet = Collection::range('a', 'z');

rsort 以降序排序数组

use Barogue\Collections\Collection;

$collection = new Collection(['a' => 9, 'b' => 1, 'c' => 5]);
$collection->reverseSort()->values(); // [9, 5, 1]

shuffle 打乱数组

use Barogue\Collections\Collection;

$collection = new Collection(['a' => 1, 'b' => 2, 'c' => 3]);
$collection->shuffle(); // ['b' => 2, 'c' => 3, 'a' => 1]
$collection->shuffle(false); // [3, 1, 2]

sizeof count的别名

use Barogue\Collections\Collection;

$collection = new Collection([1, 2, 3]);

count($collection); // 3
$collection->count(); // 3

sort 以升序排序数组

use Barogue\Collections\Collection;

$collection = new Collection(['a' => 9, 'b' => 5, 'c' => 1]);
$collection->sort()->values(); // [1, 5, 9]

uasort 使用用户定义的比较函数排序数组并保持索引关联

use Barogue\Collections\Collection;

$collection = new Collection([5, 4, 3, 2, 1]);
$collection->sortCallback(function($a, $b) {
    $aEven = $a % 2 == 0 ? 1 : 0;
    $bEven = $b % 2 == 0 ? 1 : 0;
    return $aEven === $bEven ? $a <=> $b : $aEven <=> $bEven;
}); // [4 => 1, 2 => 3, 0 => 5, 3 => 2, 1 => 4]

uksort 使用用户定义的比较函数按键排序数组

use Barogue\Collections\Collection;

$collection = new Collection([5, 4, 3, 2, 1]);
$collection->sortCallback(function($a, $b) {
    $aEven = $a % 2 == 0 ? 1 : 0;
    $bEven = $b % 2 == 0 ? 1 : 0;
    return $aEven === $bEven ? $a <=> $b : $aEven <=> $bEven;
}); // [1 => 4, 3 => 2, 0 => 5, 2 => 3, 4 => 1]

usort 使用用户定义的比较函数按值排序数组

use Barogue\Collections\Collection;

$collection = new Collection([5, 4, 3, 2, 1]);
$collection->sortCallback(function($a, $b) {
    $aEven = $a % 2 == 0 ? 1 : 0;
    $bEven = $b % 2 == 0 ? 1 : 0;
    return $aEven === $bEven ? $a <=> $b : $aEven <=> $bEven;
})->values(); // [1, 3, 5, 2, 4]