Doc #60418 [Asn->Csd]: json_encode() => Object and Array conversion behavior
| From: | frozenfire@php.net | Date: | Tue, 06 Dec 2011 23:36:35 +0000 |
| Subject: | Doc #60418 [Asn->Csd]: json_encode() => Object and Array conversion behavior | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-7605@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=60418&edit=1
ID: 60418
Updated by: frozenfire@php.net
Reported by: alain dot laporte12345 at gmail dot com
Summary: json_encode() => Object and Array conversion
behavior
-Status: Assigned
+Status: Closed
Type: Documentation Problem
Package: Documentation problem
PHP Version: 5.3.8
Assigned To: frozenfire
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2011-12-06 23:36:14] frozenfire@php.net
Automatic comment from SVN on behalf of frozenfire
Revision: http://svn.php.net/viewvc/?view=revision&revision=320527
Log: Noted non-sequential array encoding behaviour, and added example. Closes bug #60418.
------------------------------------------------------------------------
[2011-12-06 23:16:44] 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 22:37:25] 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-03 03:51:35] 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-11-30 13:24:03] 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.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=60418&edit=1