ID: 42303 User updated by: dev at lenss dot nl Reported By: dev at lenss dot nl Status: Open Bug Type: JSON related Operating System: windows, linux -PHP Version: 4.4.7 +PHP Version: 5.2.3 New Comment:
Changed version of report to PHP 5.2.3. Previous Comments: ------------------------------------------------------------------------ [2007-08-15 06:34:39] dev at lenss dot nl Description: ------------ When encoding an array with sequental keys. Everything works out fine. When encoding an array with gaps between the keys. The json encoder creates an object instead of an array. Wich maybe ideal in some places. But it's inconsistant with the JSON javascript parser at json.org. I selected the PHP 4.4.7 version. But also tested in PHP 5.2.3 with the same result. In json.c the following code is causing this: --- ext/json/json.c.orig 2007-08-14 22:38:20.000000000 +0200 +++ ext/json/json.c 2007-08-14 22:36:51.000000000 +0200 @@ -106,10 +106,6 @@ if (i == HASH_KEY_IS_STRING) { return 1; - } else { - if (index != idx) { - return 1; - } } idx++; } Reproduce code: --------------- <?php /** * Create an array with ordered keys from 0 to 1 */ $main = array(); $element1 = array(); $element2 = array(); $element1[0] = "foo"; $element2[0] = "bar"; $element2[1] = "test"; $main[0] = $element1; $main[1] = $element2; echo "Result in array: " . json_encode($main) . "\n"; /** * Create an array without ordered keys from 0 to 2 */ $main2 = array(); $element1 = array(); $element2 = array(); $element1[0] = "foo"; $element2[0] = "bar"; $element2[1] = "test"; $main2[0] = $element1; $main2[2] = $element2; echo "Result in object: " . json_encode($main2) . "\n"; ?> Expected result: ---------------- Result in array: [["foo"],["bar","test"]] Result in object: [["foo"],["bar","test"]] Actual result: -------------- Result in array: [["foo"],["bar","test"]] Result in object: {"0":["foo"],"2":["bar","test"]} ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=42303&edit=1