Doc #60418 [Fbk->Asn]: json_encode() => Object and Array conversion behavior
| From: | alain dot laporte12345 at gmail dot com | Date: | Tue, 06 Dec 2011 22:37:26 +0000 |
| Subject: | Doc #60418 [Fbk->Asn]: json_encode() => Object and Array conversion behavior | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-7603@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
User updated by: alain dot laporte12345 at gmail dot com
Reported by: alain dot laporte12345 at gmail dot com
Summary: json_encode() => Object and Array conversion
behavior
-Status: Feedback
+Status: Assigned
Type: Documentation Problem
Package: Documentation problem
PHP Version: 5.3.8
Assigned To: frozenfire
Block user comment: N
Private report: N
New Comment:
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"
Previous Comments:
------------------------------------------------------------------------
[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