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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: jmorrow at jessejmorrow dot com
New email:
PHP Version: OS:

 

 [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

Add a Patch

Pull Requests

Add a Pull Request

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 Mar 28 09:01:26 2024 UTC