Recursive iterators expose nested data one level at a time, while RecursiveIteratorIterator can flatten a hierarchy for traversal.
What Is RecursiveIteratorIterator::rewind in PHP?
Restarts traversal when supported by the wrapped iterator.
RecursiveIteratorIterator::rewind()
This operation does not accept method arguments.
Example 1: Understanding the Basic Behavior
<?php
$source = new RecursiveArrayIterator(["group" => ["first" => 1, "second" => 2], "status" => "active"]);
$iterator = new RecursiveIteratorIterator($source, RecursiveIteratorIterator::SELF_FIRST);
$iterator->rewind();
var_dump($iterator->current());
Output:
array(2) {
["first"]=>
int(1)
["second"]=>
int(2)
}
Explanation: Restarts traversal when supported by the wrapped iterator.
Example 2: Testing Another Case
<?php
$source = new RecursiveArrayIterator(["folder" => ["alpha" => 11, "beta" => 22], "state" => "pending"]);
$iterator = new RecursiveIteratorIterator($source, RecursiveIteratorIterator::SELF_FIRST);
$iterator->rewind();
var_dump($iterator->current());
Output:
array(2) {
["alpha"]=>
int(11)
["beta"]=>
int(22)
}
Explanation: Rewinding is not supported equally by every Traversable source, especially consumed generators.
Practical Example: Applying the Operation
<?php
$source = new RecursiveArrayIterator(["items" => ["red" => 100, "blue" => 200], "mode" => "ready"]);
$iterator = new RecursiveIteratorIterator($source, RecursiveIteratorIterator::SELF_FIRST);
$iterator->rewind();
var_dump($iterator->current());
Output:
array(2) {
["red"]=>
int(100)
["blue"]=>
int(200)
}
Explanation: Child operations are meaningful only for nested entries; traversal mode determines which keys and depths appear.
Important Behavior and Edge Cases
- Rewinding is not supported equally by every Traversable source, especially consumed generators.
- Child operations are meaningful only for nested entries; traversal mode determines which keys and depths appear.
Comparison With Related Approaches
| Approach | When to Use It |
|---|---|
RecursiveIteratorIterator::rewind | Restarts traversal when supported by the wrapped iterator. |
ArrayIterator | RecursiveArrayIterator exposes nested arrays, while RecursiveIteratorIterator walks through the nested levels. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Assuming every wrapped iterator can restart, even after a generator has advanced. | Rewinding is not supported equally by every Traversable source, especially consumed generators. |
| Choosing ArrayIterator without checking whether it provides the same result. | RecursiveArrayIterator exposes nested arrays, while RecursiveIteratorIterator walks through the nested levels. |
Key Takeaways
- Restarts traversal when supported by the wrapped iterator.
- Rewinding is not supported equally by every Traversable source, especially consumed generators.
- Child operations are meaningful only for nested entries; traversal mode determines which keys and depths appear.
The examples demonstrate where RecursiveIteratorIterator::rewind fits within nested iteration. Verify the relevant preconditions whenever you adapt the code to real data.