Open In App

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:
Output:
JSON elements:
[
    1,
    2,
    3,
    4,
    5
]
Program 2:
Output:
Convert the array element into JSON:
{
    "x": "geeks",
    "y": "for",
    "z": "geeks"
}
Reference: https://www.php.net/manual/en/jsonserializable.jsonserialize.php

Similar Reads