Reports the numeric position of the current iterator entry.
The examples focus on the operation itself, including the important distinction: current returns the entry value instead of its position.
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::key()
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 key 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->rewind();
$list->next();
echo $list->key();
Output from a successful local PHP 8.4 run:
1
Example 2: Checking Behavior
The list begins with 4 values. Notice how key 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->rewind();
$list->next();
echo $list->key();
Output from a successful local PHP 8.4 run:
1
Example 3: Practical Example
The list begins with 3 values. Notice how key 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->rewind();
$list->next();
echo $list->key();
Output from a successful local PHP 8.4 run:
1
Important Behavior and Edge Cases
The key depends on the current cursor and iteration direction.
Comparison with Related Approaches
| Approach | Difference |
|---|---|
| SplDoublyLinkedList::key | Reports the numeric position of the current iterator entry. |
| Related API or alternative | current returns the entry value instead of its position. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | The key depends on the current cursor and iteration direction. |
| Confusing it with a related API | current returns the entry value instead of its position. |
Key Takeaways
- Reports the numeric position of the current iterator entry.
- The key depends on the current cursor and iteration direction.
- current returns the entry value instead of its position.
Use SplDoublyLinkedList::key when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.