note 111788 added to function.json-encode

From: Date: Thu, 28 Mar 2013 11:40:16 +0000
Subject: note 111788 added to function.json-encode
Groups: php.notes 
Request: Send a blank email to php-notes+get-194244@lists.php.net to get a copy of this message
Attention when passing a plain array to json_encode and using JSON_FORCE_OBJECT. It figured out that the index-order of the resulting JSON-string depends on the system PHP is running on. $a = array("a" , "b", "c"); echo json_encode($a, JSON_FORCE_OBJECT); On Xampp (Windows) you get: {"0":"a","1":"b","2":"c"}'; On a machine running debian I get: {"2":"a","1":"b","0":"c"}'; Note that the key:value pairs are different! Solution here was to use array_combine to create a ssociative array and then pass it to json_encode: json_encode(array_combine(range(0, count($a) - 1), $a), JSON_FORCE_OBJECT); ---- Server IP: 69.147.83.199 Probable Submitter: 217.91.124.226 ---- Manual Page -- http://php.net/manual/en/function.json-encode.php Edit -- https://master.php.net/note/edit/111788 Del: integrated -- https://master.php.net/note/delete/111788/integrated Del: useless -- https://master.php.net/note/delete/111788/useless Del: bad code -- https://master.php.net/note/delete/111788/bad+code Del: spam -- https://master.php.net/note/delete/111788/spam Del: non-english -- https://master.php.net/note/delete/111788/non-english Del: in docs -- https://master.php.net/note/delete/111788/in+docs Del: other reasons-- https://master.php.net/note/delete/111788 Reject -- https://master.php.net/note/reject/111788 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#194244) next »