Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP - Ds\PriorityQueue::jsonSerialize() Function



The PHP Ds\PriorityQueue::jsonSerialize() function is used to return a representation that can be converted to JSON. We should never call this function directly.

Syntax

Below is the syntax of the PHP Ds\PriorityQueue::jsonSerialize() function −

public Ds\PriorityQueue::jsonSerialize(): array

Parameters

The jsonSerialize() function does not take any parameters.

Return Value

This function returns an array that represents the elements.

PHP Version

The jsonSerialize() function is available from version 1.0.0 of the Ds extension onwards.

Example 1

First we will show you the basic example of the PHP Ds\PriorityQueue::jsonSerialize() function to see how it works.

<?php
   // Create a new priority queue
   $queue = new \Ds\PriorityQueue();
   
   // Add some elements with priorities
   $queue->push("apple", 2);
   $queue->push("banana", 1);
   $queue->push("cherry", 3);
   
   // Convert the priority queue to JSON
   $json = json_encode($queue->jsonSerialize());
   
   // Display the JSON string
   echo $json;
?>

Output

The above code will result something like this −

["cherry","apple","banana"]

Example 2

Now we are going to use the jsonSerialize() function and handle nested priority queues and show the result.

<?php
   // Create a new PriorityQueue
   $queue1 = new \Ds\PriorityQueue();
   $queue2 = new \Ds\PriorityQueue();
   
   // Add elements to the first queue
   $queue1->push("apple", 2);
   $queue1->push("banana", 1);
   
   // Add the first queue to the second queue
   $queue2->push($queue1, 3);
   $queue2->push("cherry", 2);
   
   // Serialize the outer priority queue to JSON
   $json = json_encode($queue2->jsonSerialize());
   
   // Display the JSON string
   echo $json;
?> 

Output

This will generate the below output −

[["apple","banana"],"cherry"]

Example 3

Now the below code, we will serialize and display the elements sorted by their priority using the jsonSerialize() function, and print it.

<?php
   // Create a new PriorityQueue
   $queue = new \Ds\PriorityQueue();
   
   // Add elements with different priorities
   $queue->push("task1", 5);
   $queue->push("task2", 2);
   $queue->push("task3", 8);
   
   // Serialize the priority queue to JSON
   $json = json_encode($queue->jsonSerialize());
   
   // Display the JSON string
   echo $json;
?> 

Output

This will create the below output −

["task3","task1","task2"]

Example 4

In the following example, we are using the jsonSerialize() function to add custom objects with their priorities.

<?php
   class Task {
      public $name;
      public $designation;
  
      public function __construct($name, $designation) {
          $this->name = $name;
          $this->designation = $designation;
      }
  }
  
  // Ensure the Ds extension is enabled
  $queue = new \Ds\PriorityQueue();
  
  // Add custom objects with priorities
  $queue->push(new Task("Amit", "Engineer"), 2);
  $queue->push(new Task("Deepak", "Doctor"), 1);
  $queue->push(new Task("Sakshi", "Teacher"), 3);
  
  // Serialize the priority queue to JSON
  $json = json_encode($queue->jsonSerialize());
  
  // Display the JSON string
  echo $json;
?> 

Output

Following is the output of the above code −

[{"name":"Sakshi","designation":"Teacher"},{"name":"Amit","designation":"Engineer"},{"name":"Deepak","designation":"Doctor"}]
php_function_reference.htm
Advertisements