Change language

PHP JsonSerializable function jsonSerialize ()

Syntax:
mixed JsonSerializable::jsonSerialize (void)
Parameters:This function takes no parameters.Return value:This function returns data serialized by the json_encode() function.Below programs illustrate the JsonSerializable::jsonSerialize() function 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 ( " JSON elements: " );  
// Convert array element to JSON echo json_encode ( new vector ( $arr ), JSON_PRETTY_PRINT);  
?>
Exit:
JSON elements: [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 = [ "x" = > "engineer" , "y" = > "for" , "z" = > "engineer"]; echo ( " Convert the array element into JSON: " );  
// Convert array element to JSON echo json_encode ( new vector ( $arr ), JSON_PRETTY_PRINT);  
?>
Exit:
Convert the array element into JSON: {"x": "engineer", "y": "for", "z": "engineer"}
Link: https://www.php.net/manual/en/jsonserializable.jsonserialize.php

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method