I have the following example code in PHP:
$data = array(
'hello',
'world',
'hi'
);
$ret = array();
$ret['test'] = array();
$ret['testing'] = array();
foreach($data as $index => $value){
if($index < 1){
$ret['test'][$index]['val'] = $value;
$ret['test'][$index]['me'] = 'index < 1';
}
else {
$ret['testing'][$index]['val'] = $value;
$ret['testing'][$index]['me'] = 'index >= 1';
}
}
echo json_encode($ret);
I would expect this to be the JSON output:
[{
"test":[
{
"val": "hello",
"me": "index < 1"
}
],
"testing":[
{
"val": "world",
"me": "index >= 1"
},
{
"val": "hi",
"me": "index >= 1"
}
]
}]
However, what ends up happening is that I end up with the following:
[{
"test":[
{
"val": "hello",
"me": "index < 1"
}
],
"testing":{
"1":{
"val": "world",
"me": "index >= 1"
},
"2":{
"val": "hi",
"me": "index >= 1"
}
}
}]
The "1" and "2" keys appear despite being an int and despite the correct rendering of test when the same counter variable is used. Is there a way I can make sure that testing becomes an array of JSON objects?