Open In App

PHP SplPriorityQueue extract() Function

Last Updated : 22 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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


Similar Reads

PHP SplPriorityQueue rewind() Function
The SplPriorityQueue::rewind() function is an inbuilt function in PHP which is used to rewind the iterator back to the start position. Syntax: void SplPriorityQueue::rewind() Parameters: This function does not accept any parameter. Return Value: This function does not return any value. Example: C/C++ Code &lt;?php // Declare a class class priorityQ
1 min read
PHP SplPriorityQueue compare() Function
The SplPriorityQueue::compare() function is an inbuilt function in PHP which is used to compare the priority queue elements to place at a particular order in the heap data structure. Syntax: int SplPriorityQueue::compare( mixed $priority1 , mixed $priority2 ) Parameters: This function accepts two parameters as mentioned above and described below: p
2 min read
PHP SplPriorityQueue count() Function
The SplPriorityQueue::count() function is an inbuilt function in PHP which is used to count the number of elements in the queue. Syntax: int SplPriorityQueue::count() Parameters: This function does not accept any parameters. Return Value: This function returns the number of elements in the queue. Example: C/C++ Code &lt;?php // Declare a class clas
1 min read
PHP SplPriorityQueue current() Function
The SplPriorityQueue::current() function is an inbuilt function in PHP which is used to return the current node pointed by the iterator. Syntax: mixed SplPriorityQueue::current() Parameters: This function does not accept any parameter. Return Value: This function returns the value/priority of the current node depending on the extract flag. Example:
1 min read
PHP SplPriorityQueue insert() Function
The SplPriorityQueue::insert() function is an inbuilt function in PHP which is used to inserts an element in the queue by sifting the elements. Insert elements in priority queue by given priority. Syntax: bool SplPriorityQueue::insert( mixed $value, mixed $priority ) Parameters: This function accepts two parameters as mentioned above and described
2 min read
PHP SplPriorityQueue next() Function
The SplPriorityQueue::next() function is an inbuilt function in PHP that is used to extract the top node from the queue. Syntax: void SplPriorityQueue::next() Parameters: This function does not accept any parameter. Return Value: This function does not return any value. Example: C/C++ Code &lt;?php // Declare a class class priorityQueue extends Spl
1 min read
PHP SplPriorityQueue key() Function
The SplPriorityQueue::key() function is an inbuilt function in PHP that is used to return the current node index. Syntax: mixed SplPriorityQueue::key() Parameters: This function does not accept any parameter. Return Value: This function returns the current node index. Example: C/C++ Code &lt;?php // Declare a class class priorityQueue extends SplPr
1 min read
PHP SplPriorityQueue isEmpty() Function
The SplPriorityQueue::isEmpty() function is an inbuilt function in PHP that is used to check whether the queue is empty or not. Syntax: bool SplPriorityQueue::isEmpty() Parameters: This function does not accept any parameter. Return Value: This function returns a boolean value either true or false depending upon the queue is empty or not. Example 1
1 min read
PHP SplPriorityQueue isCorrupted() Function
The SplPriorityQueue::isCorrupted() function is an inbuilt function in PHP that is used to tells the priority queue is in a corrupted state or not. Syntax: bool SplPriorityQueue::isCorrupted() Parameters: This function does not accept any parameter. Return Value: This function returns true if the priority queue is corrupted state otherwise it retur
1 min read
PHP extract() Function
The extract() Function is an inbuilt function in PHP. The extract() function does array to variable conversion. That is it converts array keys into variable names and array values into variable value. In other words, we can say that the extract() function imports variables from an array to the symbol table.Syntax: int extract($input_array, $extract
3 min read