Something like:
[0 => 'a', 1 => 'b']
to json
{
"0": "a",
"1": "b",
}
instead of
["a","b"]
This is what you're looking for. Forcing the JSON Object is the only solution you're looking for.
$array = array( '0' => 'a', '1' => 'b', '2' => 'c', '3' => 'c' );
$json = json_encode($array, JSON_FORCE_OBJECT);
echo $json;
You can use the JSON_FORCE_OBJECT option:
$array = array(
0 => 'Banana',
1 => 'Minions',
2 => array(
5 => 'MariaOzawa',
6 => 'YukiOsawa'
)
);
$myJsonString = json_encode($MyArray, JSON_FORCE_OBJECT);
print_r($myJsonString);
Then you can see result like this:
{"0":"Banana","1":"Minions","2":{"5":"MariaOzawa","6":"YukiOsawa"}}
With this way, you can keep your array keys whatever how many layers is it into json_object
Hope this help