|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 08 13:00:02 2026 UTC |
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"