I have the following array structure:
Array
(
[t] => 812
[0] => Array
(
[5] => 649
[6] => 12
)
[2] => Array
(
[0] => 10
)
[3] => Array
(
[0] => 1
)
[4] => Array
(
[0] => 152
)
)
At the moment all the array indexes (apart from t) are integers.
I want to convert it to it’s JSON equivalent using json_encode(), but when I do so any of the arrays that have just one index in them (index 0) get converted into an integer rather than an array.
E.g.
[2] => Array
(
[0] => 10
)
gets converted to..
{"2":[10]
instead of..
{"2":[0:10]
It’d be fine for the JSON to use string indexes rather than integers if that fixed the problem..
{"2":["0":10]}
Any thoughts on how I can solve this one?
They are not converted to a single integer, but to an array with only one element!
In JSON square brackets
[]denote an array, while curly brackets{}denote an object.If you want to force
json_encodeto output objects, like in your example, you can choose the optionJSON_FORCE_OBJECT(see docu):