Tuesday, November 19, 2024
Google search engine
HomeLanguagesPHP SplPriorityQueue extract() Function

PHP SplPriorityQueue extract() Function

The SplPriorityQueue::extract() function is an inbuilt function in PHP which is used to extract a node from top of the heap and sift up.

Syntax:

mixed SplPriorityQueue::extract()

Parameters: This function does not accept any parameter.

Return Value: This function returns the value/priority (or both) of the extracted node depending on the extract flag. 

 

Example:

PHP




<?php
  
// Declare a class
class priorityQueue extends SplPriorityQueue {
      
    // Compare function to compare priority
    // queue elements
    public function compare($p1, $p2) {
        if ($p1 === $p2) return 0;
        return $p1 < $p2 ? -1 : 1;
    }
}
  
// Create an object of priority queue
$obj = new priorityQueue();
  
// Insert elements into the queue
$obj->insert("Geeks",2);
$obj->insert("GFG",1);
$obj->insert("G4G",3);
$obj->insert('G',4);
  
// Display the extracted element
// from priority queue
var_dump($obj->extract());
  
?>


Output

string(1) "G"

Reference: https://www.php.net/manual/en/splpriorityqueue.extract.php

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments