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 below:
- $value: This parameter holds the value that need to inset in priority queue.
- $priority: This parameter holds the priority of priority queue.
Return Value: This function returns true if elements inserted successfully.
Example:
PHP
<?php
class priorityQueue extends SplPriorityQueue {
public function compare( $p1 , $p2 ) {
if ( $p1 === $p2 ) return 0;
return $p1 < $p2 ? -1 : 1;
}
}
$obj = new priorityQueue();
$obj ->insert( "Geeks" ,2);
$obj ->insert( "GFG" ,1);
$obj ->insert( "G4G" ,3);
$obj ->insert( 'G' ,4);
var_dump( $obj );
?>
|
Output
object(priorityQueue)#1 (3) {
["flags":"SplPriorityQueue":private]=>
int(1)
["isCorrupted":"SplPriorityQueue":private]=>
bool(false)
["heap":"SplPriorityQueue":private]=>
array(4) {
[0]=>
array(2) {
["data"]=>
string(1) "G"
["priority"]=>
int(4)
}
[1]=>
array(2) {
["data"]=>
string(3) "G4G"
["priority"]=>
int(3)
}
[2]=>
array(2) {
["data"]=>
string(5) "Geeks"
["priority"]=>
int(2)
}
[3]=>
array(2) {
["data"]=>
string(3) "GFG"
["priority"]=>
int(1)
}
}
}
Reference: https://www.php.net/manual/en/splpriorityqueue.insert.php
Last Updated :
22 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...