PHP | JsonSerializable jsonSerialize() Function Last Updated : 27 Sep, 2019 Comments Improve Suggest changes Like Article Like Report The JsonSerializable::jsonSerialize() function is an inbuilt function in PHP which is used to serialize the JSON object to a value that can be serialized natively by using json_encode() function. Syntax: mixed JsonSerializable::jsonSerialize( void ) Parameters: This function does not accept any parameters. Return Value: This function returns the data which is serialized by json_encode() function. Below programs illustrate the JsonSerializable::jsonSerialize() function in PHP: Program 1: php <?php class vector implements JsonSerializable { public function __construct(array $arr) { $this->array = $arr; } public function jsonSerialize() { return $this->array; } } // Declare an array $arr = [1, 2, 3, 4, 5]; echo("JSON elements:\n"); // Convert the array element into JSON echo json_encode(new vector($arr), JSON_PRETTY_PRINT); ?> Output: JSON elements: [ 1, 2, 3, 4, 5 ] Program 2: php <?php class vector implements JsonSerializable { public function __construct(array $arr) { $this->array = $arr; } public function jsonSerialize() { return $this->array; } } // Declare an array $arr = [ "x" => "geeks", "y" => "for", "z" => "geeks" ]; echo("Convert the array element into JSON:\n"); // Convert the array element into JSON echo json_encode(new vector($arr), JSON_PRETTY_PRINT); ?> Output: Convert the array element into JSON: { "x": "geeks", "y": "for", "z": "geeks" } Reference: https://www.php.net/manual/en/jsonserializable.jsonserialize.php Comment More infoAdvertise with us Next Article PHP | JsonSerializable jsonSerialize() Function J jit_t Follow Improve Article Tags : Web Technologies PHP PHP-function JSON Similar Reads How to use serialize() and unserialize() Function in PHP? In PHP, complex data can not be transported or can not be stored. If you want to execute continuously a complex set of data beyond a single script then these serialize() and unserialize() functions are handy to deal with those complex data structures. The serialize() function just gives a compatible 2 min read PHP | DsVector jsonSerialize() Function The Ds\Vector::jsonSerialize() function is an inbuilt function in PHP which is used to return the element which can be converted to JSON. Syntax: mixed public JsonSerializable::jsonSerialize( void ) Parameters: This function does not accepts any parameter. Return Value: This function returns the val 1 min read PHP SplObjectStorage unserialize() Function The SplObjectStorage::unserialize() function is an inbuilt function in PHP which is used to unserialize the storage from its serialize string representation. Syntax: void SplObjectStorage::unserialize( $serilize ) Parameters: This function accepts a single parameter $serialize which specifies the st 1 min read PHP SplObjectStorage serialize() Function The SplObjectStorage::serialize() function is an inbuilt function in PHP which is used to serialize the result of the storage. Syntax: string SplObjectStorage::serialize() Parameters: This function does not accept any parameter. Return Value: This function returns a string which is the representatio 1 min read PHP | SplDoublyLinkedList unserialize() Function The SplDoublyLinkedList::unserialize() function is an inbuilt function in PHP which is used to unserialize the storage of SplDoublyLinkedList::serialize() function. Syntax: void SplDoublyLinkedList::unserialize( string $serialized ) Parameters: This function accepts single parameter $serialized whic 2 min read Like