Edit report at http://bugs.php.net/bug.php?id=53201&edit=1

 ID:                 53201
 Updated by:         ras...@php.net
 Reported by:        pratik dot vanol at gmail dot com
 Summary:            json_encode can not encode as array when passing
                     array with key
-Status:             Open
+Status:             Wont fix
 Type:               Feature/Change Request
 Package:            JSON related
 Operating System:   ALL
 PHP Version:        5.3.3
 Block user comment: N

 New Comment:

Well, that is how json works.  If you simply want to drop the keys and
get an 

array of just the values, just do:



json_encode(array_values($array))


Previous Comments:
------------------------------------------------------------------------
[2010-10-29 13:46:13] pratik dot vanol at gmail dot com

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 this bug report at http://bugs.php.net/bug.php?id=53201&edit=1

Reply via email to