php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #60418 json_encode() => Object and Array conversion behavior
Submitted: 2011-11-30 13:24 UTC Modified: 2011-12-06 23:36 UTC
From: alain dot laporte12345 at gmail dot com Assigned: frozenfire (profile)
Status: Closed Package: Documentation problem
PHP Version: 5.3.8 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: alain dot laporte12345 at gmail dot com
New email:
PHP Version: OS:

 

 [2011-11-30 13:24 UTC] alain dot laporte12345 at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/function.json-encode#refsect1-function.json-
encode-notes
---

The behavior about conversion in object when an array have no consecutive keys is 
not clear in the documentation.

We must use array_values() function to reinitialize keys, before to call 
json_encode().

This behavior should be explicited in the "notes" section.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-12-03 03:51 UTC] frozenfire@php.net
-Status: Open +Status: Feedback -Assigned To: +Assigned To: frozenfire
 [2011-12-03 03:51 UTC] frozenfire@php.net
I'm unsure what behaviour exactly that you're indicating needs to be documented. 
Could you please provide some example code, so that we do not need to guess at 
what needs to be documented?
 [2011-12-06 22:37 UTC] alain dot laporte12345 at gmail dot com
-Status: Feedback +Status: Assigned
 [2011-12-06 22:37 UTC] alain dot laporte12345 at gmail dot com
Here a example code :

#!/usr/bin/php
<?php
$toto = array("toto", "titi", "tata");
var_dump($toto);

var_dump(json_encode($toto));

unset($toto[1]);

var_dump($toto);

var_dump(json_encode($toto));
?>

The output :


array(3) {
  [0]=>
  string(4) "toto"
  [1]=>
  string(4) "titi"
  [2]=>
  string(4) "tata"
}
string(22) "["toto","titi","tata"]"
array(2) {
  [0]=>
  string(4) "toto"
  [2]=>
  string(4) "tata"
}
string(23) "{"0":"toto","2":"tata"}"

The first json_encode convert $toto to an array. And the second, after delete 
the second element, convert $toto to an 
object.

This behavior must be strange when we don't know the JSON definition about array 
and object, explicited here => http://www.json.org/
 * "An object is an unordered set of name/value pairs"
 * "An array is an ordered collection of values"
 [2011-12-06 23:16 UTC] frozenfire@php.net
That's a very interesting behaviour that I wasn't aware of.

http://svn.php.net/viewvc/php/php-src/trunk/ext/json/json.c?
revision=318178&view=markup#l175

This section of code essentially says that if any of the keys are not in 
numerical order, the keys are all treated as strings.

I'll definitely document this.
 [2011-12-06 23:36 UTC] frozenfire@php.net
Automatic comment from SVN on behalf of frozenfire
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=320527
Log: Noted non-sequential array encoding behaviour, and added example. Closes bug #60418.
 [2011-12-06 23:36 UTC] frozenfire@php.net
-Status: Assigned +Status: Closed
 [2011-12-06 23:36 UTC] frozenfire@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Oct 08 13:00:02 2026 UTC