MultipleIterator combines values from multiple iterators at matching iteration positions.
What Is MultipleIterator::attachIterator in PHP?
Adds an iterator to a MultipleIterator so its values can be traversed alongside others.
MultipleIterator::attachIterator(Iterator $iterator, string|int|null $info = ...)
Parameters:
$iterator— Required. The iterator to attach or traverse.$info— Optional. The info argument used to configure this operation; its accepted values follow the declared PHP type.
Example 1: Understanding the Basic Behavior
<?php
$iterator = new MultipleIterator(MultipleIterator::MIT_NEED_ANY);
$firstIterator = new ArrayIterator([1, 2]);
$secondIterator = new ArrayIterator(['a', 'b']);
$iterator->attachIterator($firstIterator, "numbers");
$iterator->attachIterator($secondIterator, "letters");
$thirdIterator = new ArrayIterator([true, false]);
$iterator->attachIterator($thirdIterator, "flags");
var_dump($iterator->countIterators());
Output:
int(3)
Explanation: Adds an iterator to a MultipleIterator so its values can be traversed alongside others.
Example 2: Testing Another Case
<?php
$iterator = new MultipleIterator(MultipleIterator::MIT_NEED_ANY);
$firstIterator = new ArrayIterator([5, 6]);
$secondIterator = new ArrayIterator(['c', 'd']);
$iterator->attachIterator($firstIterator, "numbers");
$iterator->attachIterator($secondIterator, "letters");
$thirdIterator = new ArrayIterator([true, false]);
$iterator->attachIterator($thirdIterator, "flags");
var_dump($iterator->countIterators());
Output:
int(3)
Explanation: Attached iterators advance together; adding a source does not concatenate values.
Practical Example: Applying the Operation
<?php
$iterator = new MultipleIterator(MultipleIterator::MIT_NEED_ANY);
$firstIterator = new ArrayIterator([8, 9]);
$secondIterator = new ArrayIterator(['e', 'f']);
$iterator->attachIterator($firstIterator, "numbers");
$iterator->attachIterator($secondIterator, "letters");
$thirdIterator = new ArrayIterator([true, false]);
$iterator->attachIterator($thirdIterator, "flags");
var_dump($iterator->countIterators());
Output:
int(3)
Explanation: The MIT_NEED_ALL and MIT_NEED_ANY options change when iteration stops; keys may be positional or associated.
Important Behavior and Edge Cases
- Attached iterators advance together; adding a source does not concatenate values.
- The MIT_NEED_ALL and MIT_NEED_ANY options change when iteration stops; keys may be positional or associated.
Comparison With Related Approaches
| Approach | When to Use It |
|---|---|
MultipleIterator::attachIterator | Adds an iterator to a MultipleIterator so its values can be traversed alongside others. |
ArrayIterator | AppendIterator concatenates iterators sequentially; MultipleIterator reads several iterators in parallel. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Expecting MultipleIterator to concatenate sequences rather than advance attached iterators together. | Attached iterators advance together; adding a source does not concatenate values. |
| Choosing ArrayIterator without checking whether it provides the same result. | AppendIterator concatenates iterators sequentially; MultipleIterator reads several iterators in parallel. |
Key Takeaways
- Adds an iterator to a MultipleIterator so its values can be traversed alongside others.
- Attached iterators advance together; adding a source does not concatenate values.
- The MIT_NEED_ALL and MIT_NEED_ANY options change when iteration stops; keys may be positional or associated.
The examples demonstrate where MultipleIterator::attachIterator fits within synchronized iteration. Verify the relevant preconditions whenever you adapt the code to real data.