|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45162 JSON does not properly encode Arrays
Submitted: 2008-06-03 15:35 UTC Modified: 2008-06-05 18:23 UTC
From: christopher dot nagle at umb dot edu Assigned:
Status: Not a bug Package: JSON related
PHP Version: 5.2.6 OS: Windows XP/ Ubuntu
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: christopher dot nagle at umb dot edu
New email:
PHP Version: OS:


 [2008-06-03 15:35 UTC] christopher dot nagle at umb dot edu
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);
echo json_encode((array) $m);

Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2008-06-05 18:23 UTC]
This is totally expected behaviour. Please read this:

Note: array and assoc-array are different things. Latter being "object" in json.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Jun 18 09:01:29 2024 UTC