ZipIteratorIterator

Class

Combines multiple iterators into a single iterator, returning arrays of grouped values.

Description

This iterator synchronously iterates over multiple traversable sources, yielding an array where each element corresponds to the current value of each source. The iteration stops when the shortest iterator is exhausted.

Usage Example:

Tags
example

Description

Zipping two iterators

use FastForward\Iterator\ZipIteratorIterator;
use ArrayIterator;

$dataSetOne = new ArrayIterator([1, 2, 3]);
$dataSetTwo = new ArrayIterator(['A', 'B', 'C']);

$zippedIterator = new ZipIteratorIterator($dataSetOne, $dataSetTwo);

foreach ($zippedIterator as $pair) {
    print_r($pair);
}
// Outputs:
// [1, 'A']
// [2, 'B']
// [3, 'C']

Note: The iterator stops when the shortest iterable is exhausted.

since
1.0.0

Table of Contents

Properties

 : array<int, Iterator>

Methods

__construct()

Initializes the ZipIteratorIterator.

 : mixed
count()

Counts the number of elements available in the iterator.

 : int
current()

Retrieves the current set of values from each iterator.

 : array<int, mixed>
key()

Retrieves the current key (index) of the iteration.

 : int
next()

Moves to the next set of values in each iterator.

 : void
rewind()

Resets the iterator to the beginning.

 : void
valid()

Checks if all iterators still have valid elements.

 : bool
Properties

$currentIndex

Private
private int $currentIndex = 0

Description

the current iteration index

$iterators

Private Read-only
private array<int, Iterator> $iterators

Description

the list of active iterators

Methods

__construct()

Public

Initializes the ZipIteratorIterator.

public __construct( iterable<string|int, mixed>  ...$iterators) : mixed
Parameters
$iterators : iterable<string|int, mixed>

Description

The iterators to be combined.

Tags
throws
InvalidArgumentException

Description

if fewer than two iterators are provided

count()

Public

Counts the number of elements available in the iterator.

public count() : int

Description

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

Description

the total number of elements exposed by the iterator

current()

Public

Retrieves the current set of values from each iterator.

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

Description

the array of current values from each iterator

key()

Public

Retrieves the current key (index) of the iteration.

public key() : int
Return values
int

Description

the current index

next()

Public

Moves to the next set of values in each iterator.

public next() : void

valid()

Public

Checks if all iterators still have valid elements.

public valid() : bool

Description

The iteration stops when the shortest iterator is exhausted.

Return values
bool

Description

true if valid elements exist, false otherwise