дромру/iterable-chunk

用于将可迭代集合分割为分块的类。

v2.0.0 2023-01-23 02:23 UTC

This package is auto-updated.

Last update: 2024-09-23 06:43:33 UTC


README

Latest Stable Version Tests Coverage Status Minimum PHP Version

问题

原生的 array_chunk 不支持 iterable。Guzzle 的 ChunkedIterator 实现在速度上不如本实现。

解决方案

iterable-chunk 是一个提供类来将可迭代集合 iterable 分割为分块的包。

备注:使用生成器实现,因此比 ChunkedIterator 快 8 倍。

基准测试

使用示例

function getIterable(): iterable
{
    yield from [1, 2, 3, 4, 5];
}

$chunks = new IterableChunk(getIterable(), 2, true);

foreach ($chunks as $chunk) {
    print_r($chunk);
}
[0 => 1, 1 => 2]
[2 => 3, 3 => 4]
[4 => 5]