From: Operating system: Windows PHP version: 5.2.13 Package: JSON related Bug Type: Bug Bug description:casting json objects to array
Description: ------------ We use this array: array(2) { [1]=> int(100) [2]=> int(200) } When a array will be encoded into a json string, php converts integer indexes t o strings: string(17) "{"1":100,"2":200}" When php decodes the json string back, it will be an object: object(stdClass)#1 (2) { ["1"]=> int(100) ["2"]=> int(200) } To avoid the problem that you not can acces integer indexes of a object you must cast the object to an array: array(2) { ["1"]=> int(100) ["2"]=> int(200) } Now the indexes become strings instead of integers, which you cannot access: $arr2[1] = NULL The json_decode function should convert indexes back to integer, if they are a string number, like php always does when you create an array. Test script: --------------- $arr = array(1=>100,2=>200,"1"=>100,"2"=>200); $json = json_encode($arr); $arr2 = (array)json_decode($json); var_dump($arr2); var_dump($arr2[1]); var_dump($arr2["1"]); Expected result: ---------------- array(4) { [1]=> int(100) [2]=> int(200) } int(100) int(100) Actual result: -------------- array(4) { ["1"]=> int(100) ["2"]=> int(200) } NULL NULL -- Edit bug report at http://bugs.php.net/bug.php?id=51635&edit=1 -- Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=51635&r=trysnapshot52 Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=51635&r=trysnapshot53 Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=51635&r=trysnapshot60 Fixed in SVN: http://bugs.php.net/fix.php?id=51635&r=fixed Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=51635&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=51635&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=51635&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=51635&r=needscript Try newer version: http://bugs.php.net/fix.php?id=51635&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=51635&r=support Expected behavior: http://bugs.php.net/fix.php?id=51635&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=51635&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=51635&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=51635&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=51635&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=51635&r=dst IIS Stability: http://bugs.php.net/fix.php?id=51635&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=51635&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=51635&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=51635&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=51635&r=mysqlcfg