json_decode
Decode strings in JSON format
The json_decode()
function is used to decode or convert JSON objects into PHP objects.
Store JSON data in a PHP variable and decode it into a PHP object:
<?php $jsonobj = '{"Bill":35,"Elon":37,"Steve":43}' ; var_dump ( json_decode ( $jsonobj ) ) ; ?>
Run the instance
Store the JSON data in a PHP variable and decode it into a PHP associative array:
<?php $jsonobj = '{"Bill":35,"Elon":37,"Steve":43}' ; var_dump ( json_decode ( $jsonobj , true ) ) ; ?>
Run the instance
How to access values from PHP objects:
<?php $jsonobj = '{"Bill":35,"Elon":37,"Steve":43}' ; $obj = json_decode ( $jsonobj ) ; echo $obj -> Bill ; echo $obj -> Elon ; echo $obj -> Joe ; ?>
Run the instance
How to access values from PHP associative arrays:
<?php $jsonobj = '{"Bill":35,"Elon":37,"Steve":43}' ; $arr = json_decode ( $jsonobj , true ) ; echo $arr [ "Bill" ] ; echo $arr [ "Elon" ] ; echo $arr [ "Steve" ] ; ?>
Run the instance
json_decode ( string , assoc , depth , options )
parameter | describe |
---|---|
string | Required. Specifies the value to decode. |
assoc |
Optional. Specifies a boolean value. The default is false. When set to true, the returned object will be converted to an associative array. When set to false, it returns an object. |
depth | Optional. Specifies the recursive depth. The default recursion depth is 512. |
options | Optional. Specify bitmasks (JSON_BIGINT_AS_STRING, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_OBJECT_AS_ARRAY, JSON_THROW_ON_ERROR) |