Skip to content

InterleaveIteratorIterator

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

Interleaves elements from multiple iterators in a round-robin fashion.

This iterator alternates between multiple traversable sources, returning one element from each before cycling back to the first. The iteration stops once all iterators are exhausted.

Usage Example:


Methods

__construct

Initializes the InterleaveIteratorIterator.

public __construct(iterable $iterators): mixed

Parameters:

Parameter Type Description
$iterators iterable The iterators to be interleaved.

Throws:

if no iterators are provided


current

Retrieves the current element from the active iterator.

public current(): mixed

Return Value:

the current element


key

Retrieves the current key from the active iterator or a normalized sequential key.

public key(): string|int

If the active iterator's current key is a string, it is returned directly. Otherwise, a normalized sequential numeric key is returned based on the position of yielded values without string keys.

Return Value:

the current key


next

public next(): void

valid

Checks if at least one iterator still has elements.

public valid(): bool

Return Value:

true if there are remaining elements, false otherwise


rewind

public rewind(): void

Inherited methods

count

Counts the number of elements available in the iterator.

public count(): int

This method MUST count the elements by iterating over a clone of the current iterator instance so that the active iterator state of the original object is not modified during the counting process. Concrete implementations SHOULD therefore remain safely cloneable whenever this behavior is expected to be used.

Return Value:

the total number of elements exposed by the iterator


Clone this wiki locally