Skip to content

GroupByIteratorIterator

github-actions edited this page Mar 31, 2026 · 3 revisions

Class GroupByIteratorIterator.

Groups elements from an iterator based on a callback function.

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:


Methods

__construct

Initializes the GroupByIteratorIterator.

public __construct(iterable $iterator, \Closure $groupBy): mixed

Parameters:

Parameter Type Description
$iterator iterable the iterator containing values to be grouped
$groupBy \Closure a function that determines the group key for each element

rewind

Rewinds the iterator and reprocesses the grouping.

public rewind(): void

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


current

Retrieves the current group of elements.

public current(): array<int,mixed>

Return Value:

the current group of elements


key

Retrieves the key of the current group.

public key(): mixed

Return Value:

the computed key representing the current group


next

Advances to the next group in the iterator.

public next(): void

valid

Checks if the current position is valid.

public valid(): bool

Return Value:

true if a valid group exists, false otherwise


Inherited methods

count

Counts the number of elements in the iterable.

public count(): int

If the inner iterator implements Countable, it uses that. Otherwise, it counts the elements by iterating through them.

Return Value:

the number of elements in the iterable


Clone this wiki locally