Skip to content

InterleaveIteratorIterator

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

Class InterleaveIteratorIterator.

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 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