Open In App

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:




<?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);
  
// Use key() function to get the 
// current key of priority queue
var_dump($obj->key());
  
?>

Output
int(3)

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

Article Tags :