json_decode() Function in PHP

Q

Where to get the detailed description of the json_decode() Function in PHP?

✍: FYIcenter.com

A

Here is the detailed description of the json_decode() Function in PHP.

Description - The json_decode() function parses a JSON text string and converts it into a PHP variable.

Syntax -

mixed json_decode(string $json[, bool $assoc=false[, int $depth=512[, int $options=0]]])

Parameters -

  • $json - Required. The string to be decoded as JSON text string.
  • $assoc - Optional. When TRUE, the returned variable will be converted into associative arrays.
  • $depth - Optional. User specified recursion depth.
  • $options - Optional. Bitmask of JSON decode options. Currently there are two supported options. The first is JSON_BIGINT_AS_STRING that allows casting big integers to string instead of floats which is the default. The second option is JSON_OBJECT_AS_ARRAY that has the same effect as setting assoc to TRUE.

Return value - PHP value in appropriate data type representing the JSON text string. NULL is returned if the $json cannot be decoded or if the encoded data is deeper than the recursion limit.

Examples -

<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));

# Output:
#
#   object(stdClass)#1 (5) {
#       ["a"] => int(1)
#       ["b"] => int(2)
#       ["c"] => int(3)
#       ["d"] => int(4)
#       ["e"] => int(5)
#   }
#   
#   array(5) {
#       ["a"] => int(1)
#       ["b"] => int(2)
#       ["c"] => int(3)
#       ["d"] => int(4)
#       ["e"] => int(5)
#   }
?>

 

json_decode() - JSON to PHP Data Type Mapping

What Is PHP JSON Extension

Using JSON in PHP

⇑⇑ JSON Tutorials

2023-02-28, 1263🔥, 0💬