#45162 [Opn->Bgs]: JSON does not properly encode Arrays
| From: | jani@php.net | Date: | Thu, 05 Jun 2008 18:23:35 +0000 |
| Subject: | #45162 [Opn->Bgs]: JSON does not properly encode Arrays | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-125532@lists.php.net to get a copy of this message | ||
ID: 45162
Updated by: jani@php.net
Reported By: christopher dot nagle at umb dot edu
-Status: Open
+Status: Bogus
Bug Type: JSON related
Operating System: Windows XP/ Ubuntu
PHP Version: 5.2.6
New Comment:
This is totally expected behaviour. Please read this:
http://www.json.org/
Note: array and assoc-array are different things. Latter being "object"
in json.
Previous Comments:
------------------------------------------------------------------------
[2008-06-03 15:35:39] christopher dot nagle at umb dot edu
Description:
------------
After manipulating arrays using [] or creating an array in the form
array("key" => value, ...) or calling unset on an array element.
json_encode will encode the array as an object rather than an array.
Reproduce code:
---------------
echo json_encode(array(1 => new stdClass(), 2 => new stdClass(), 3 =>
new stdClass()));
$m = array();
$m[1] = new stdClass();
$m[2] = new stdClass();
$m[3] = new stdClass();
echo json_encode($m);
$m = array_values($m);
echo json_encode($m);
unset($m[0]);
echo json_encode((array) $m);
Expected result:
----------------
["1":{},"2":{},"3":{}]
["1":{},"2":{},"3":{}]
[{},{},{}]
["1":{},"2":{}]
Actual result:
--------------
{"1":{},"2":{},"3":{}}
{"1":{},"2":{},"3":{}}
[{},{},{}]
{"1":{},"2":{}}
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=45162&edit=1