Req #53201 [NEW]: json_encode can not encode as array when passing array with key
| From: | pratik dot vanol at gmail dot com | Date: | Fri, 29 Oct 2010 11:46:13 +0000 |
| Subject: | Req #53201 [NEW]: json_encode can not encode as array when passing array with key | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-155625@lists.php.net to get a copy of this message | ||
From:
Operating system: ALL
PHP version: 5.3.3
Package: JSON related
Bug Type: Feature/Change Request
Bug description:json_encode can not encode as array when passing array with key
Description:
------------
Hi,
I am having one issues with json_encode function. Let me explain briefly.
I want the json represented string from PHP Array. When I pass the array
with not actually given 'key' value. json_encode return the encoded string
as array.
But after giving the array with associative array it is forcefully
generated encoded as an object.
In short :
I can forcefully get the output of json_encode as an Object (using
JSON_FORCE_OBJECT as 2nd Argument))
But can not forcefully get the output of json_encode as an Array (!!!?)
I really need josn_encode as an array in my functionality no matter key is
given or not. Like added the JSON_FORCE_OBJECT there must be something for
array.
ignore keys should be pass in optional argument.
Please do not say this is actual behavior of json. I need this solution in
anyhow.
Test script:
---------------
<?php
/*I am giving array with no keys or numeric start with 0,2,3,4....with
break sequence*/
$array = array("0" => "value1", "1" => "value2",
"2" => "value3", "3" =>
"value4");
echo "Array output as array: ", json_encode($array);
echo "<br />";
/*I am giving array with no keys or numeric start with 0,2,3,4....with
break sequence*/
$array = array("0" => "value1", "3" => "value2",
"3" => "value3", "6" =>
"value4");
echo "Array output should be array: ", json_encode($array); // Need output
as an Array here shows as an Object forcefully
Expected result:
----------------
//Expected result:
Array output as array :
["value1","value2","value3","value4"]
Array output should be array:
["value1","value2","value3","value4"]
Actual result:
--------------
//Actual result:
Array output as array: ["value1","value2","value3","value4"]
Array output should be array:
{"0":"value1","3":"value3","6":"value4"}
--
Edit bug report at http://bugs.php.net/bug.php?id=53201&edit=1
--
Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=53201&r=trysnapshot52
Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=53201&r=trysnapshot53
Try a snapshot (trunk): http://bugs.php.net/fix.php?id=53201&r=trysnapshottrunk
Fixed in SVN: http://bugs.php.net/fix.php?id=53201&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=53201&r=needdocs
Fixed in release: http://bugs.php.net/fix.php?id=53201&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=53201&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=53201&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=53201&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=53201&r=support
Expected behavior: http://bugs.php.net/fix.php?id=53201&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=53201&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=53201&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=53201&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=53201&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=53201&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=53201&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=53201&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=53201&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=53201&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=53201&r=mysqlcfg