Content · Web Development · PHP

PHP SplPriorityQueue::rewind() Method: Usage and Examples

Implements Iterator::rewind as a no-op on the priority queue. Previously extracted entries are not restored, and iteration continues to consume the remaining queue.

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

Implements Iterator::rewind as a no-op on the priority queue.

The examples focus on the operation itself, including the important distinction: next removes the current greatest-priority item.

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


SplPriorityQueue::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


Extracting consumes the top job; rewind cannot restore an already extracted priority-queue entry.

<?php
$priority = new SplPriorityQueue();
$priority->insert('job-4', 4);
$priority->insert('job-12', 12);
$priority->insert('job-7', 7);
$removed = $priority->extract();
$priority->rewind();
echo json_encode(["removed" => $removed, "current" => $priority->current()]);

Output from a successful local PHP 8.4 run:

{"removed":"job-12","current":"job-7"}

Example 2: Checking Behavior


Extracting consumes the top job; rewind cannot restore an already extracted priority-queue entry.

<?php
$priority = new SplPriorityQueue();
$priority->insert('job-2', 2);
$priority->insert('job-9', 9);
$priority->insert('job-5', 5);
$priority->insert('job-1', 1);
$removed = $priority->extract();
$priority->rewind();
echo json_encode(["removed" => $removed, "current" => $priority->current()]);

Output from a successful local PHP 8.4 run:

{"removed":"job-9","current":"job-5"}

Example 3: Practical Example


Extracting consumes the top job; rewind cannot restore an already extracted priority-queue entry.

<?php
$priority = new SplPriorityQueue();
$priority->insert('job-19', 19);
$priority->insert('job-3', 3);
$priority->insert('job-11', 11);
$removed = $priority->extract();
$priority->rewind();
echo json_encode(["removed" => $removed, "current" => $priority->current()]);

Output from a successful local PHP 8.4 run:

{"removed":"job-19","current":"job-11"}

Important Behavior and Edge Cases


Previously extracted entries are not restored, and iteration continues to consume the remaining queue.

Comparison with Related Approaches


ApproachDifference
SplPriorityQueue::rewindImplements Iterator::rewind as a no-op on the priority queue.
Related API or alternativenext removes the current greatest-priority item.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the operation-specific limitsPreviously extracted entries are not restored, and iteration continues to consume the remaining queue.
Confusing it with a related APInext removes the current greatest-priority item.

Key Takeaways


  • Implements Iterator::rewind as a no-op on the priority queue.
  • Previously extracted entries are not restored, and iteration continues to consume the remaining queue.
  • next removes the current greatest-priority item.

Use SplPriorityQueue::rewind 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.