#38945 [NEW]: json_decode has limits on array depth.

From: Date: Mon, 25 Sep 2006 07:42:12 +0000
Subject: #38945 [NEW]: json_decode has limits on array depth.
Groups: php.doc 
Request: Send a blank email to phpdoc+get-969374091@lists.php.net to get a copy of this message
From: RQuadling at GMail dot com Operating system: Windows XP SP2 PHP version: Irrelevant PHP Bug Type: Documentation problem Bug description: json_decode has limits on array depth. Description: ------------ Massively nested arrays are decoded as null by json_decode. Admittedly, an array this deep has its own concerns, but with no warnings or notices, documentation of the limit would be advisable. The following code has 2 arrays, the first is 18 levels, the second is 19. The first decodes correctly, the second decodes as null. 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 that's why this is just a minor doc issue and not a more important code issue. 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). It also makes no difference if you use the 'assoc' option (json_decode's second paramater of True) to produce an associative array. The limit is still reached at 19 levels. Reproduce code: --------------- <?php $a_array = array('One' => array('Two' => array('Three' => array('Four' => array('Five' => array('Six' => array('Seven' => array('Eight' => array('Nine' => array('Ten' => array('Eleven' => array('Twelve' => array('Thirteen' => array('Fourteen' => array('Fifteen' => array('Sixteen' => array('Seventeen' => array('Eighteen' => array(1,2,3))))))))))))))))))); var_dump(json_encode($a_array)); var_dump(json_decode(json_encode($a_array))); $a_array = array('One' => array('Two' => array('Three' => array('Four' => array('Five' => array('Six' => array('Seven' => array('Eight' => array('Nine' => array('Ten' => array('Eleven' => array('Twelve' => array('Thirteen' => array('Fourteen' => array('Fifteen' => array('Sixteen' => array('Seventeen' => array('Eighteen' => array('Nineteen' => array(1,2,3)))))))))))))))))))); var_dump(json_encode($a_array)); var_dump(json_decode(json_encode($a_array))); ?> Expected result: ---------------- string(195) "{"One":{"Two":{"Three":{"Four":{"Five":{"Six":{"Seven":{"Eight":{"Nine":{"Ten":{"Eleven":{"Twelve":{"Thirteen":{"Fourteen":{"Fifteen":{"Sixteen":{"Seventeen":{"Eighteen":[1,2,3]}}}}}}}}}}}}}}}}}}" object(stdClass)#1 (1) { ["One"]=> object(stdClass)#2 (1) { ["Two"]=> object(stdClass)#3 (1) { ["Three"]=> object(stdClass)#4 (1) { ["Four"]=> object(stdClass)#5 (1) { ["Five"]=> object(stdClass)#6 (1) { ["Six"]=> object(stdClass)#7 (1) { ["Seven"]=> object(stdClass)#8 (1) { ["Eight"]=> object(stdClass)#9 (1) { ["Nine"]=> object(stdClass)#10 (1) { ["Ten"]=> object(stdClass)#11 (1) { ["Eleven"]=> object(stdClass)#12 (1) { ["Twelve"]=> object(stdClass)#13 (1) { ["Thirteen"]=> object(stdClass)#14 (1) { ["Fourteen"]=> object(stdClass)#15 (1) { ["Fifteen"]=> object(stdClass)#16 (1) { ["Sixteen"]=> object(stdClass)#17 (1) { ["Seventeen"]=> object(stdClass)#18 (1) { ["Eighteen"]=> array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } } } } } } } } } } } } } } } } } } } string(208) "{"One":{"Two":{"Three":{"Four":{"Five":{"Six":{"Seven":{"Eight":{"Nine":{"Ten":{"Eleven":{"Twelve":{"Thirteen":{"Fourteen":{"Fifteen":{"Sixteen":{"Seventeen":{"Eighteen":{"Nineteen":[1,2,3]}}}}}}}}}}}}}}}}}}}" object(stdClass)#1 (1) { ["One"]=> object(stdClass)#2 (1) { ["Two"]=> object(stdClass)#3 (1) { ["Three"]=> object(stdClass)#4 (1) { ["Four"]=> object(stdClass)#5 (1) { ["Five"]=> object(stdClass)#6 (1) { ["Six"]=> object(stdClass)#7 (1) { ["Seven"]=> object(stdClass)#8 (1) { ["Eight"]=> object(stdClass)#9 (1) { ["Nine"]=> object(stdClass)#10 (1) { ["Ten"]=> object(stdClass)#11 (1) { ["Eleven"]=> object(stdClass)#12 (1) { ["Twelve"]=> object(stdClass)#13 (1) { ["Thirteen"]=> object(stdClass)#14 (1) { ["Fourteen"]=> object(stdClass)#15 (1) { ["Fifteen"]=> object(stdClass)#16 (1) { ["Sixteen"]=> object(stdClass)#17 (1) { ["Seventeen"]=> object(stdClass)#18 (1) { ["Eighteen"]=> object(stdClass)#19 (1) { ["Nineteen"] => array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } } } } } } } } } } } } } } } } } } } } Actual result: -------------- string(195) "{"One":{"Two":{"Three":{"Four":{"Five":{"Six":{"Seven":{"Eight":{"Nine":{"Ten":{"Eleven":{"Twelve":{"Thirteen":{"Fourteen":{"Fifteen":{"Sixteen":{"Seventeen":{"Eighteen":[1,2,3]}}}}}}}}}}}}}}}}}}" object(stdClass)#1 (1) { ["One"]=> object(stdClass)#2 (1) { ["Two"]=> object(stdClass)#3 (1) { ["Three"]=> object(stdClass)#4 (1) { ["Four"]=> object(stdClass)#5 (1) { ["Five"]=> object(stdClass)#6 (1) { ["Six"]=> object(stdClass)#7 (1) { ["Seven"]=> object(stdClass)#8 (1) { ["Eight"]=> object(stdClass)#9 (1) { ["Nine"]=> object(stdClass)#10 (1) { ["Ten"]=> object(stdClass)#11 (1) { ["Eleven"]=> object(stdClass)#12 (1) { ["Twelve"]=> object(stdClass)#13 (1) { ["Thirteen"]=> object(stdClass)#14 (1) { ["Fourteen"]=> object(stdClass)#15 (1) { ["Fifteen"]=> object(stdClass)#16 (1) { ["Sixteen"]=> object(stdClass)#17 (1) { ["Seventeen"]=> object(stdClass)#18 (1) { ["Eighteen"]=> array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } } } } } } } } } } } } } } } } } } } string(208) "{"One":{"Two":{"Three":{"Four":{"Five":{"Six":{"Seven":{"Eight":{"Nine":{"Ten":{"Eleven":{"Twelve":{"Thirteen":{"Fourteen":{"Fifteen":{"Sixteen":{"Seventeen":{"Eighteen":{"Nineteen":[1,2,3]}}}}}}}}}}}}}}}}}}}" NULL -- Edit bug report at http://bugs.php.net/?id=38945&edit=1 -- Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=38945&r=trysnapshot44 Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=38945&r=trysnapshot52 Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=38945&r=trysnapshot60 Fixed in CVS: http://bugs.php.net/fix.php?id=38945&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=38945&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=38945&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=38945&r=needscript Try newer version: http://bugs.php.net/fix.php?id=38945&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=38945&r=support Expected behavior: http://bugs.php.net/fix.php?id=38945&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=38945&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=38945&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=38945&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=38945&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=38945&r=dst IIS Stability: http://bugs.php.net/fix.php?id=38945&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=38945&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=38945&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=38945&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=38945&r=mysqlcfg

« previous php.doc (#969374091) next »