PHP json_decode depth parameter doesn't work
The documentation says why.
NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.
the problem here is that you didn't understand the depth
parameter correctly
the depth of your test
array is 3 and so it will not be printed in the first two iterations and a null
value is returned
but in the 3rd iteration it gets printed because its depth is equal to the $depth
[i.e. 3]
In addition to @Explosion Pills answer, you expect json_decode
to work as json_encode
should.
According to documentation you can now specify your own limit, to encode arrays/objects. Which simply means that it will skip going deeper than specified level.
For json_decode
it's different - it always try to parse whole JSON string, because it simply can't stop, and skip deeper parts without parsing the whole string. This is why depth limit causes the function to return NULL in this cases.
json_encode
can stop and skip deeper parts, as the data structure is already defined in the memory.
Notice that $depth
for json_encode
was added for PHP version 5.5.0 (json_decode
has it since 5.3.0). Check changelog here.