GroupByIteratorIterator

Class

Groups elements from an iterator based on a callback function.

Description

This iterator aggregates elements into associative arrays where the keys are determined by a user-defined function ($groupBy). Each key contains an array of elements that share the same computed group.

Usage Example:

Tags
example

Description

Grouping by age

use FastForward\Iterator\GroupByIteratorIterator;
use ArrayIterator;

$data = new ArrayIterator([
    ['name' => 'Alice', 'age' => 25],
    ['name' => 'Bob', 'age' => 30],
    ['name' => 'Charlie', 'age' => 25],
]);

$grouped = new GroupByIteratorIterator($data, fn($item) => $item['age']);

foreach ($grouped as $age => $group) {
    echo "Age: $age\n";
    print_r($group);
}
// Outputs:
// Age: 25
// [['name' => 'Alice', 'age' => 25], ['name' => 'Charlie', 'age' => 25]]
// Age: 30
// [['name' => 'Bob', 'age' => 30]]

Note: The iterator must be rewound before iterating again to ensure correct grouping.

since
1.0.0

Table of Contents

Properties

 : Closure
 : array<string|int, array<int, mixed>>

Methods

__construct()

Initializes the GroupByIteratorIterator.

 : mixed
count()

Counts the number of elements exposed by the inner iterator.

 : int
current()

Retrieves the current group of elements.

 : array<int, mixed>
key()

Retrieves the key of the current group.

 : mixed
next()

Advances to the next group in the iterator.

 : void
rewind()

Rewinds the iterator and reprocesses the grouping.

 : void
valid()

Checks if the current position is valid.

 : bool
Properties

$groups

Private
private array<string|int, array<int, mixed>> $groups = []

Description

holds grouped elements

Methods

__construct()

Public

Initializes the GroupByIteratorIterator.

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

Description

the iterator containing values to be grouped

$groupBy : Closure

Description

a function that determines the group key for each element

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

current()

Public

Retrieves the current group of elements.

public current() : array<int, mixed>
Return values
array<int, mixed>

Description

the current group of elements

key()

Public

Retrieves the key of the current group.

public key() : mixed
Return values

Description

the computed key representing the current group

rewind()

Public

Rewinds the iterator and reprocesses the grouping.

public rewind() : void

Description

This ensures that the grouping is correctly recomputed when the iterator is reset.

valid()

Public

Checks if the current position is valid.

public valid() : bool
Return values
bool

Description

true if a valid group exists, false otherwise