From:             dev at lenss dot nl
Operating system: windows, linux
PHP version:      4.4.7
PHP Bug Type:     JSON related
Bug description:  json encode creates objects from array with gaps in the index

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 bug report at http://bugs.php.net/?id=42303&edit=1
-- 
Try a CVS snapshot (PHP 4.4): 
http://bugs.php.net/fix.php?id=42303&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): 
http://bugs.php.net/fix.php?id=42303&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=42303&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=42303&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=42303&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=42303&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=42303&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=42303&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=42303&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=42303&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=42303&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=42303&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=42303&r=globals
PHP 3 support discontinued:   http://bugs.php.net/fix.php?id=42303&r=php3
Daylight Savings:             http://bugs.php.net/fix.php?id=42303&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=42303&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=42303&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=42303&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=42303&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=42303&r=mysqlcfg

Reply via email to