Content · Web Development · PHP

PHP SplDoublyLinkedList::setIteratorMode() Method: Usage and Examples

Changes FIFO/LIFO traversal and KEEP/DELETE behavior with a combined bitmask. DELETE iteration consumes values; choose KEEP to retain entries.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Changes FIFO/LIFO traversal and KEEP/DELETE behavior with a combined bitmask.

The examples focus on the operation itself, including the important distinction: getIteratorMode reports the current bitmask.

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::setIteratorMode(int $mode)

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.

Parameters


  • mode: required.

Example 1: Basic Use


The list begins with 3 values. Notice how setIteratorMode 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->setIteratorMode(SplDoublyLinkedList::IT_MODE_LIFO | SplDoublyLinkedList::IT_MODE_KEEP);
echo json_encode(iterator_to_array($list));

Output from a successful local PHP 8.4 run:

{"2":"water","1":"coffee","0":"tea"}

Example 2: Checking Behavior


The list begins with 4 values. Notice how setIteratorMode 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->setIteratorMode(SplDoublyLinkedList::IT_MODE_LIFO | SplDoublyLinkedList::IT_MODE_KEEP);
echo json_encode(iterator_to_array($list));

Output from a successful local PHP 8.4 run:

{"3":"four","2":"three","1":"two","0":"one"}

Example 3: Practical Example


The list begins with 3 values. Notice how setIteratorMode 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->setIteratorMode(SplDoublyLinkedList::IT_MODE_LIFO | SplDoublyLinkedList::IT_MODE_KEEP);
echo json_encode(iterator_to_array($list));

Output from a successful local PHP 8.4 run:

{"2":12,"1":8,"0":4}

Important Behavior and Edge Cases


DELETE iteration consumes values; choose KEEP to retain entries.

Comparison with Related Approaches


ApproachDifference
SplDoublyLinkedList::setIteratorModeChanges FIFO/LIFO traversal and KEEP/DELETE behavior with a combined bitmask.
Related API or alternativegetIteratorMode reports the current bitmask.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the operation-specific limitsDELETE iteration consumes values; choose KEEP to retain entries.
Confusing it with a related APIgetIteratorMode reports the current bitmask.

Key Takeaways


  • Changes FIFO/LIFO traversal and KEEP/DELETE behavior with a combined bitmask.
  • DELETE iteration consumes values; choose KEEP to retain entries.
  • getIteratorMode reports the current bitmask.

Use SplDoublyLinkedList::setIteratorMode when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.

Share this resourceSend this page to someone who may find it useful.