json_decode
Description
The json_decode of JSON for PHP decodes a JSON string.
Syntax
json_decode( string $json, ?bool $associative = null, int $depth = 512, int $flags = 0 ): mixed
Parameters
json
The json string being decoded.
This function only works with UTF-8 encoded strings.
NOTE: PHP implements a superset of JSON as specified in the original RFC 7159.
associative
When true, JSON objects will be returned as associative arrays; when false, JSON objects will be returned as objects. When null, JSON objects will be returned as associative arrays or objects depending on whether JSON_OBJECT_AS_ARRAY is set in the flags.
depth
Maximum nesting depth of the structure being decoded.
The value must be greater than 0, and less than or equal to 2147483647.
flags
Bitmask of JSON_BIGINT_AS_STRING, JSON_OBJECT_AS_ARRAY, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_THROW_ON_ERROR.
Return
Returns the value encoded in json as an appropriate PHP type. Unquoted values true, false and null are returned as true, false and null respectively. null is returned if the json cannot be decoded or if the encoded data is deeper than the nesting limit.
Examples
1 · json
<? $value = [ "a" => 1, "b" => 2, "c" => 3 ]; $json = json_encode($value); echo $json . PHP_EOL; $return = json_decode($json); print_r($return);
{"a":1,"b":2,"c":3} stdClass Object ( [a] => 1 [b] => 2 [c] => 3 )
2 · associative · false
<? $value = [ "a" => 1, "b" => 2, "c" => 3 ]; $json = json_encode($value); echo $json . PHP_EOL; $associative = false; $return = json_decode($json, $associative); print_r($return);
{"a":1,"b":2,"c":3} stdClass Object ( [a] => 1 [b] => 2 [c] => 3 )
3 · associative · true
<? $value = [ "a" => 1, "b" => 2, "c" => 3 ]; $json = json_encode($value); echo $json . PHP_EOL; $associative = true; $return = json_decode($json, $associative); print_r($return);
{"a":1,"b":2,"c":3} Array ( [a] => 1 [b] => 2 [c] => 3 )
4 · depth
<? $value = [ 1, 2, [ 1, 2, [ 1, 2 ] ] ]; $json = json_encode($value); echo $json . PHP_EOL; $associative = null; $depth = 4; $return = json_decode($json, $associative, $depth); print_r($return);
[1,2,[1,2,[1,2]]] Array ( [0] => 1 [1] => 2 [2] => Array ( [0] => 1 [1] => 2 [2] => Array ( [0] => 1 [1] => 2 ) ) )
5 · flags · 0
<? $value = [ 1, 2, 3 ]; $json = json_encode($value); echo $json . PHP_EOL; $associative = null; $depth = 512; $flags = 0; $return1 = json_decode($json); $return2 = json_decode($json, $associative, $depth, $flags); print_r($return1); print_r($return2);
[1,2,3] Array ( [0] => 1 [1] => 2 [2] => 3 ) Array ( [0] => 1 [1] => 2 [2] => 3 )
6 · flags · JSON_BIGINT_AS_STRING
<? $json = '{"a":12345678901234567890}'; echo $json . PHP_EOL; $associative = null; $depth = 512; $flags = JSON_BIGINT_AS_STRING; $return1 = json_decode($json); $return2 = json_decode($json, $associative, $depth, $flags); print_r($return1); print_r($return2);
{"a":12345678901234567890} stdClass Object ( [a] => 1.2345678901235E+19 ) stdClass Object ( [a] => 12345678901234567890 )
7 · flags · JSON_OBJECT_AS_ARRAY
<? $value = [ "a" => 1, "b" => 2, "c" => 3 ]; $json = json_encode($value); echo $json . PHP_EOL; $associative = null; $depth = 512; $flags = JSON_OBJECT_AS_ARRAY; $return1 = json_decode($json); $return2 = json_decode($json, $associative, $depth, $flags); print_r($return1); print_r($return2);
{"a":1,"b":2,"c":3} stdClass Object ( [a] => 1 [b] => 2 [c] => 3 ) Array ( [a] => 1 [b] => 2 [c] => 3 )