Home > Article > Backend Development > Convert json string to object in php?
In PHP, you can use the json_decode() function to convert a json string into a PHP object. The json_decode() function is used to decode a JSON string and convert the json string into an object or array. By default, it is converted into an object. If the second parameter of the function is set to true, it can be converted into an associative array.
json_decode() function is a built-in function in PHP, used to decode JSON format strings and convert JSON format strings into PHP Variable (object or array). [Related tutorial recommendations: "PHP Tutorial"]
The syntax is:
json_decode( $json, $assoc = FALSE, $depth = 512, $options = 0 )
Parameters: The The function accepts four parameters:
#json: It contains the JSON string that needs to be decoded. It only works with UTF-8 encoded strings.
assoc: This is a Boolean variable. If true, the returned object will be converted to an associative array.
depth: Specifies the user-specified recursion depth.
options: Bitmask containing JSON_OBJECT_AS_ARRAY, JSON_BIGINT_AS_STRING, JSON_THROW_ON_ERROR.
Return value: This function returns the encoded JSON value in the appropriate PHP type. Returns NULL if the json cannot be decoded or the depth of the encoded data is greater than the recursion limit.
Example:
When the second parameter is TRUE, an array will be returned, and when FALSE, an object will be returned.
The following are the execution results:
The above is the detailed content of Convert json string to object in php?. For more information, please follow other related articles on the PHP Chinese website!