#42303 [Opn->Fbk]: json encode creates objects from array with gaps in the index
| From: | jani@php.net | Date: | Wed, 15 Aug 2007 08:45:14 +0000 |
| Subject: | #42303 [Opn->Fbk]: json encode creates objects from array with gaps in the index | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-116096@lists.php.net to get a copy of this message | ||
ID: 42303
Updated by: jani@php.net
Reported By: dev at lenss dot nl
-Status: Open
+Status: Feedback
Bug Type: JSON related
Operating System: windows, linux
PHP Version: 5.2.3
New Comment:
Please try using this CVS snapshot:
http://snaps.php.net/php5.2-latest.tar.gz
For Windows (zip):
http://snaps.php.net/win32/php5.2-win32-latest.zip
For Windows (installer):
http://snaps.php.net/win32/php5.2-win32-installer-latest.msi
Previous Comments:
------------------------------------------------------------------------
[2007-08-15 08:42:51] dev at lenss dot nl
Changed version of report to PHP 5.2.3.
------------------------------------------------------------------------
[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