PHP Ds / Vector jsonSerialize () function

PHP

Syntax:
mixed public JsonSerializable::jsonSerialize (void)
Parameters: This function takes no parameters.Return Value:This function returns vector values ​​in a form that can be converted to JSON.The following programs illustrate the function Ds / Vector::jsonSerialize()in PHP:Program 1: class vector implements JsonSerializable { public function __ construct ( array $arr ) { $this -> array = $arr ; } public function jsonSerialize() { return $this -> array ; } }  
// Declare the array $arr = [1, 2, 3, 4, 5]; echo ( " Elements after converting to JSON convertible form " ); echo json_encode ( new vector ( $arr ), JSON_PRETTY_PRINT);  
?>
Output:
Elements after converting to JSON convertible form [1, 2, 3, 4, 5]
Program 2: class vector implements JsonSerializable { public function __ construct ( array $arr ) { $this -> array = $arr ; } public function jsonSerialize() { return $this -> array ; } }  
// Declare the array $arr = [ "engineer" , "for" , "engineer" ]; echo ( " Elements after converting to JSON convertible form " ); echo json_encode ( new vector ( $arr ), JSON_PRETTY_PRINT);  
?>
Output:
Elements after converting to JSON convertible form ["engineer", "for", "engineer"]
Link: http://php.net/manual/en/ds-vector.jsonserialize.php



Get Solution for free from DataCamp guru