Positions the iterator on the first entry for its configured traversal direction.
The examples focus on the operation itself, including the important distinction: next advances; rewind positions at the traversal start.
What It Does
Use the examples below to observe the behavior on a small dataset before extending it to application code.
Syntax and Return Value
SplDoublyLinkedList::rewind()
The displayed signature describes the API or identifies any correction or optional dependency. When a parameter is optional, supply it only when the task requires it.
Example 1: Basic Use
The list begins with 3 values. Notice how rewind changes or observes its indexed deque rather than an ordinary associative array.
<?php
$list = new SplDoublyLinkedList();
$list->push('tea');
$list->push('coffee');
$list->push('water');
$list->next();
$list->rewind();
echo json_encode($list->current());
Output from a successful local PHP 8.4 run:
"tea"
Example 2: Checking Behavior
The list begins with 4 values. Notice how rewind changes or observes its indexed deque rather than an ordinary associative array.
<?php
$list = new SplDoublyLinkedList();
$list->push('one');
$list->push('two');
$list->push('three');
$list->push('four');
$list->next();
$list->rewind();
echo json_encode($list->current());
Output from a successful local PHP 8.4 run:
"one"
Example 3: Practical Example
The list begins with 3 values. Notice how rewind changes or observes its indexed deque rather than an ordinary associative array.
<?php
$list = new SplDoublyLinkedList();
$list->push(4);
$list->push(8);
$list->push(12);
$list->next();
$list->rewind();
echo json_encode($list->current());
Output from a successful local PHP 8.4 run:
4
Important Behavior and Edge Cases
With LIFO iteration, the initial entry is the last physical list element.
Comparison with Related Approaches
| Approach | Difference |
|---|---|
| SplDoublyLinkedList::rewind | Positions the iterator on the first entry for its configured traversal direction. |
| Related API or alternative | next advances; rewind positions at the traversal start. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | With LIFO iteration, the initial entry is the last physical list element. |
| Confusing it with a related API | next advances; rewind positions at the traversal start. |
Key Takeaways
- Positions the iterator on the first entry for its configured traversal direction.
- With LIFO iteration, the initial entry is the last physical list element.
- next advances; rewind positions at the traversal start.
Use SplDoublyLinkedList::rewind when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.