php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41505 json_encode() improperly encodes PHP arrays with an empty string array key
Submitted: 2007-05-26 07:33 UTC Modified: 2009-11-13 14:52 UTC
From: jmorrow at jessejmorrow dot com Assigned: iliaa (profile)
Status: Closed Package: JSON related
PHP Version: 5.2.2 OS: OS X
Private report: No CVE-ID: None
 [2007-05-26 07:33 UTC] jmorrow at jessejmorrow dot com
Description:
------------
The json_encode() function completely ignores, and thus does not encode, 
PHP array entries with an array key that is an empty string: array
(""=>"value");

Empty string array keys and object properties are totally valid PHP and 
JSON respectively - they should encode/decode as such.

This, along with the corresponding json_decode() bug of a similar nature  
(41504), are show stoppers for me as I need JSON encode/decode to be 
rock solid and consistent.

Thanks for looking into this :-)

Reproduce code:
---------------
1) var_dump(json_encode(array(""=>"value")));
2) var_dump(json_encode(array(""=>"value1", "key2"=>"value2")));

Expected result:
----------------
1) string(15) "{"":"value"}"
2) string(17) "{"":"value1", "key2":"value2"}"

Actual result:
--------------
1) string(2) "{}"
2) string(17) "{"key2":"value2"}"

In both cases the array entry with the empty string key is missing.

This might be addressed by Bug #41034, but may very well be a different 
bug.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-05-27 16:32 UTC] iliaa@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 13:01:29 2024 UTC