Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

SplPriorityQueue

Associate data with priorities and extract the highest-priority entry first. setExtractFlags chooses data, priority or both.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

new SplPriorityQueue()

Associate data with priorities and extract the highest-priority entry first. setExtractFlags chooses data, priority or both. Equal priorities have no defined order, so model a tie-breaker if ordering matters. Iteration removes items; iterate a clone to keep the queue.

$q=new SplPriorityQueue(); $q->insert("later",1); $q->insert("first",7); $q->setExtractFlags(SplPriorityQueue::EXTR_BOTH);
$copy=clone $q; $items=[]; foreach($copy as $row){$items[]=$row;} echo json_encode([$items,count($q),count($copy)]);
[[{"data":"first","priority":7},{"data":"later","priority":1}],2,0]

Examples

<?php
$q=new SplPriorityQueue(); $q->insert("later",1); $q->insert("first",7); $q->setExtractFlags(SplPriorityQueue::EXTR_BOTH);
$copy=clone $q; $items=[]; foreach($copy as $row){$items[]=$row;} echo json_encode([$items,count($q),count($copy)]);

Result

[[{"data":"first","priority":7},{"data":"later","priority":1}],2,0]

Sources