• PHP Video Tutorials

PHP - Ds Vector jsonSerialize() Function



Ds\Vector::jsonSerialize() function can return a representation that can be converted to JSON.

Syntax

mixed public JsonSerializable::jsonSerialize( void )

Ds\Vector::jsonSerialize() function doesn't have any parameters. This function can return the values of vector in a form that can be converted to JSON.

Example 1

<?php 
   class vector implements JsonSerializable { 
      public function __construct(array $arr) { 
         $this->array = $arr; 
      } 
      public function jsonSerialize() {
         return $this->array; 
      } 
   } 
   $array1 = [1, 2, 3, 4, 5]; 
  
   echo("The elements after converting to JSON \n"); 
   echo json_encode(new vector($array1), JSON_PRETTY_PRINT); 
?>

Example 2

<?php 
   class vector implements JsonSerializable { 
      public function __construct(array $arr) { 
         $this->array = $arr; 
      } 
      public function jsonSerialize() { 
         return $this->array; 
      } 
   } 
   $array1 = ["Tutorials", "Point", "Tutorix"]; 
  
   echo("The elements after converting to JSON \n"); 
   echo json_encode(new vector($array1), JSON_PRETTY_PRINT); 
?>
php_function_reference.htm
Advertisements