3v4l.org

run code in 300+ PHP versions simultaneously
<?php $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(); //Sets the mode of extraction (EXTR_DATA, EXTR_PRIORITY, EXTR_BOTH) $pq->setExtractFlags(SplPriorityQueue::EXTR_BOTH); //go at the node from the top of the queue $clone1 = clone $pq; $clone1->top(); //iterate the queue (by priority) and display each element while ($clone1->valid()) { print_r($clone1->current()); $clone1->next(); } $clone2 = clone $pq; $clone2->top(); //iterate the queue (by priority) and display each element while ($clone2->valid()) { print_r($clone2->current()); $clone2->next(); }

preferences:
41.79 ms | 402 KiB | 5 Q