PHPackages                             abordji/php-generator-chunk - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. abordji/php-generator-chunk

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

abordji/php-generator-chunk
===========================

A utility function to split a PHP Generator into chunks of a specified length, returning a Generator of Generators, with optional key preservation.

1.0(1y ago)03MITPHPPHP &gt;=8.1CI passing

Since Apr 17Pushed 1y ago1 watchersCompare

[ Source](https://github.com/abordji/php-generator-chunk)[ Packagist](https://packagist.org/packages/abordji/php-generator-chunk)[ RSS](/packages/abordji-php-generator-chunk/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (3)Versions (3)Used By (0)

PHP Generator Chunk
===================

[](#php-generator-chunk)

A utility function to split a PHP Generator into chunks of a specified length, returning a Generator of Generators, with optional key preservation.

Description
-----------

[](#description)

This library provides a single function, `generator_chunk`, that allows you to efficiently process large datasets by splitting a PHP Generator into smaller, manageable chunks. This is particularly useful when dealing with data that cannot fit into memory all at once.

Features
--------

[](#features)

- **Chunking:** Splits a Generator into chunks of a specified length.
- **Key Preservation:** Optionally preserves the original keys of the Generator.
- **Generator of Generators:** Returns a Generator where each element is itself a Generator.
- **Usage Considerations:** Requires careful handling of the returned Generator of Generators. Directly using `iterator_to_array` is **not recommended**.
- **Error Handling:** Throws a `ValueError` if the chunk length is invalid (less than or equal to 0).
- **PHP 8.1+:** Requires PHP 8.1 or later due to the use of `ValueError`.

Requirements
------------

[](#requirements)

- **PHP 8.1 or later** (Requirement for the code itself)
- **Docker** (for the development and testing environment)
- **Docker Compose** (for orchestrating the Docker environment)
- **Make** (for automating development tasks)

Installation
------------

[](#installation)

You can install this library using Composer:

```
composer require abordji/php-generator-chunk
```

(If you are contributing to this library, please refer to the Contributing section for development setup instructions. You do not need Composer on your host machine to contribute.)

API
---

[](#api)

### `generator_chunk(Generator $generator, int $length, bool $preserve_keys = false): Generator`

[](#generator_chunkgenerator-generator-int-length-bool-preserve_keys--false-generator)

Splits a PHP Generator into chunks of a specified length.

- `$generator` (*Generator*): The Generator to be chunked.
- `$length` (*int*): The length of each chunk. Must be greater than 0.
- `$preserve_keys` (*bool*, optional): Whether to preserve the original keys. Defaults to `false`.

**Returns:** (*Generator*) A Generator where each yielded element is itself a Generator containing the items for that chunk.

**Throws:** `ValueError` if `$length` is less than or equal to 0.

Usage
-----

[](#usage)

```
