Content · Web Development · PHP

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

Selects EXTR_DATA, EXTR_PRIORITY or EXTR_BOTH for future extraction. The flag changes return shape, not the internal priority ordering.

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

Selects EXTR_DATA, EXTR_PRIORITY or EXTR_BOTH for future extraction.

The examples focus on the operation itself, including the important distinction: getExtractFlags reads the configured mode.

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::setExtractFlags(int $flags)

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


  • flags: required.

Example 1: Basic Use


The queue is a max-priority queue; equal priorities have no guaranteed FIFO order. The setExtractFlags call is shown on a small, inspectable dataset.

<?php
$heap = new SplPriorityQueue();
$heap->insert('task-4', 4);
$heap->insert('task-12', 12);
$heap->insert('task-7', 7);
$heap->setExtractFlags(SplPriorityQueue::EXTR_BOTH);
echo json_encode($heap->extract());

Output from a successful local PHP 8.4 run:

{"data":"task-12","priority":12}

Example 2: Checking Behavior


The queue is a max-priority queue; equal priorities have no guaranteed FIFO order. The setExtractFlags call is shown on a small, inspectable dataset.

<?php
$heap = new SplPriorityQueue();
$heap->insert('task-2', 2);
$heap->insert('task-9', 9);
$heap->insert('task-5', 5);
$heap->insert('task-1', 1);
$heap->setExtractFlags(SplPriorityQueue::EXTR_BOTH);
echo json_encode($heap->extract());

Output from a successful local PHP 8.4 run:

{"data":"task-9","priority":9}

Example 3: Practical Example


The queue is a max-priority queue; equal priorities have no guaranteed FIFO order. The setExtractFlags call is shown on a small, inspectable dataset.

<?php
$heap = new SplPriorityQueue();
$heap->insert('task-19', 19);
$heap->insert('task-3', 3);
$heap->insert('task-11', 11);
$heap->setExtractFlags(SplPriorityQueue::EXTR_BOTH);
echo json_encode($heap->extract());

Output from a successful local PHP 8.4 run:

{"data":"task-19","priority":19}

Important Behavior and Edge Cases


The flag changes return shape, not the internal priority ordering.

Comparison with Related Approaches


ApproachDifference
SplPriorityQueue::setExtractFlagsSelects EXTR_DATA, EXTR_PRIORITY or EXTR_BOTH for future extraction.
Related API or alternativegetExtractFlags reads the configured mode.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the operation-specific limitsThe flag changes return shape, not the internal priority ordering.
Confusing it with a related APIgetExtractFlags reads the configured mode.

Key Takeaways


  • Selects EXTR_DATA, EXTR_PRIORITY or EXTR_BOTH for future extraction.
  • The flag changes return shape, not the internal priority ordering.
  • getExtractFlags reads the configured mode.

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