Speed up development with full-stack environments for every branch.

Learn More

How to implement the SplPriorityQueue class [PHP]

421 Runs 1232 Views 398 Copies


gtsolutions 373

published 4 years ago


 * PHP SPL - How to implement the SplPriorityQueue class

$pq = new SplPriorityQueue();

//insert method inserts an element in the queue by shifting it up
$pq->insert('A', 3);
$pq->insert('B', 6);
$pq->insert('C', 1);
$pq->insert('D', 2);

//count the elements
echo "count ->" . $pq->count() . "<br />";

//Sets the mode of extraction (EXTR_DATA, EXTR_PRIORITY, EXTR_BOTH)

//go at the node from the top of the queue

//iterate the queue (by priority) and display each element
while ($pq->valid()) {
    echo "<br />";
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).