PHPackages                             double-break/php-batcher - 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. double-break/php-batcher

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

double-break/php-batcher
========================

Simple lib providing batching functionality

1.0.2(1y ago)00MITPHPPHP ^8.4

Since Apr 16Pushed 1y ago1 watchersCompare

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

READMEChangelogDependencies (1)Versions (4)Used By (0)

PHP Batcher
===========

[](#php-batcher)

**PHP Batcher** is a simple library providing batching functionality for PHP projects.

Features
--------

[](#features)

- Efficient batching of items.
- Ordinality tracking (e.g., first, middle, last batch).
- Custom state management whiten the batch processing.

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

[](#requirements)

- PHP 8.4 or higher.

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

[](#installation)

Install the library using Composer:

```
composer require double-break/php-batcher
```

Usage
-----

[](#usage)

### Basic Example

[](#basic-example)

Here is an example of how to use the library:

```
use DoubleBreak\PhpBatcher\Batcher\Generic;

$batcher = new Generic(5, function (array $batch) {
    // Process the batch
    echo "Processing batch: " . implode(", ", $batch) . PHP_EOL;
});

$batcher->add("Item 1");
$batcher->add("Item 2");
$batcher->add("Item 3");
$batcher->add("Item 4");
$batcher->add("Item 5"); // Important: WILL NOT trigger the flush callback
$batcher->add("Item 6"); // This triggers the automatic flush before adding the new item

$batcher->flush(); // Manually flush remaining items
```

### Example with Ordinality Tracking

[](#example-with-ordinality-tracking)

This example demonstrates how to track the ordinality of batches (e.g., first, middle, last):

```
