It also makes no difference if you use the 'assoc' option (json_decode's second paramater of True) to produce an associative array.
#Php json decode depth how to#
How to encode data into JSON in PHP In PHP, the jsonencode() function is used to encode data into JSON. These function jsonencode() is used to encode data into JSON and jsondecode() is used to decode data from JSON, respectively. PHP has built-in functions for encoding and decoding JSON data. The output of the above code is: PHP JSON. If it is true then objects returned will be converted into associative arrays. It only works with UTF-8 encoded strings. json: It holds the JSON string which need to be decode. The issue is with json_decode as json_encode quite happily encodes the larger array - an inconsistency maybe as the json_decode dox does say any json_encode'd output (though I am awaiting the changes due to bug#38680). A JSON data structure looks a lot like a PHP array. jsondecode(string, assoc, depth500, options) Parameters of jsondecode() function. Sure, not exactly LEAN, but that's why this is just a minor doc issue and not a more important code issue. Whilst an array MAY not be this deep, it is possible that objects containing objects with arrays COULD produce a depth of 19 levels. Sure, not exactly LEAN, but thats why this is just a minor doc issue and not a more important code issue. The first decodes correctly, the second decodes as null. The first decodes correctly, the second decodes as null. Here’s some to be aware of: JSONFORCEOBJECT Convert PHP numerical arrays to JSON objects instead of arrays. Many more flags are supported when encoding data. The following code has 2 arrays, the first is 18 levels, the second is 19. Take care around the order though in a quirk of the PHP standard library, the position of these two optional parameters is switched compared to jsondecode().
Massively nested arrays are decoded as null by json_decode.Īdmittedly, an array this deep has its own concerns, but with no warnings or notices, documentation of the limit would be advisable.