php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #53201 json_encode can not encode as array when passing array with key
Submitted: 2010-10-29 13:46 UTC Modified: 2010-10-30 11:06 UTC
Votes:4
Avg. Score:3.0 ± 1.4
Reproduced:2 of 3 (66.7%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: pratik dot vanol at gmail dot com Assigned:
Status: Wont fix Package: JSON related
PHP Version: 5.3.3 OS: ALL
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2010-10-29 13:46 UTC] 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"}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-10-29 16:55 UTC] rasmus@php.net
-Status: Open +Status: Wont fix
 [2010-10-29 16:55 UTC] rasmus@php.net
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))
 [2010-10-30 11:06 UTC] pratik dot vanol at gmail dot com
Thanks for your reply...
Yeah, We can do the extra coding for removing array keys. Your suggestion is work for one dimensional array. We can also do this from recursive function and removing keys in all level depth. 

But it might taken extra time for executing the script if array is large.

I have currently solved my problem using the recursive functions. But I hope it should be something there for forcefully array JSON in PHP function that I strongly believe. Look that if you genius guys can do this in PHP functions. 

Further this is required because many scripts worked with only array of JSON.

Thanks for your co-operation.

Pratik
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jul 20 21:01:29 2024 UTC