Skip to content

GroupByIteratorIterator

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

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 exposed by the inner iterator.

public count(): int

If the inner iterator implements

  • See: \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

  • See: \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 Value:

the total number of elements available from the inner iterator


Clone this wiki locally