ChunkedIteratorAggregate

Class

Splits an iterable into fixed-size chunks.

Description

This iterator wraps a Traversable and groups elements into subarrays of a fixed size. If the total number of elements is not a multiple of the chunk size, the last chunk may contain fewer elements.

Usage Example:

Tags
example

Description

Using ChunkedIteratorAggregate

use FastForward\Iterator\ChunkedIteratorAggregate;

$chunks = new ChunkedIteratorAggregate(range(1, 10), 3);

foreach ($chunks as $chunk) {
    print_r($chunk);
}
// Outputs:
// [1, 2, 3]
// [4, 5, 6]
// [7, 8, 9]
// [10]
since
1.0.0

Table of Contents

Properties

 : int
 : iterable<string|int, mixed>

Methods

__construct()

Initializes the ChunkedIteratorAggregate.

 : mixed
count()

Counts the number of elements exposed by the inner iterator.

 : int
getIterator()

Retrieves an iterator that yields arrays containing elements in chunks.

 : Traversable<int, array<int, mixed>>
Properties
Methods

__construct()

Public

Initializes the ChunkedIteratorAggregate.

public __construct( iterable<string|int, mixed>  $iterator, int  $chunkSize) : mixed
Parameters
$iterator : iterable<string|int, mixed>

Description

the iterator containing values to be chunked

$chunkSize : int

Description

the number of elements per chunk (must be >= 1)

count()

Public

Counts the number of elements exposed by the inner iterator.

public count() : int

Description

If the inner iterator implements Countable, this method SHALL return the value provided by that implementation. Otherwise, it MUST count elements by iterating over the iterator. If the inner iterator is not cloneable, this method SHALL wrap the current object in an IteratorIterator instance and count through that wrapper to avoid performing an invalid clone operation. If the inner iterator is cloneable, this method SHOULD count over a clone so that the original iterator state is preserved as much as possible.

Return values
int

Description

the total number of elements available from the inner iterator

getIterator()

Public

Retrieves an iterator that yields arrays containing elements in chunks.

public getIterator() : Traversable<int, array<int, mixed>>

Description

The iteration groups elements from the original iterator into subarrays of $chunkSize elements each.

Return values
Traversable<int, array<int, mixed>>

Description

the iterator yielding chunked arrays