Here is an example of what I need. I need to append the results
key at the beginning of my json results
JSON
{
"results": [ <-- this is what I need
{
"id": 1,
"text": "Option 1"
},
{
"id": 2,
"text": "Option 2"
}
]
}
PHP
I am creating my json results as such with php
$sql = "SELECT * FROM table";
$result = $GLOBALS['db']->query($sql);
$i = 0;
while ($row = $result->fetch_array()) {
$response[$i]['id'] = $row['id'];
$response[$i]['text'] = $row['text'];
$data['posts'][$i] = $response[$i];
$i++;
}
if ($result->num_rows == 0) {
echo '[]';
} else {
echo json_encode($data['posts']);
}
Which produces
[
{
"id": 1,
"text": "Option 1"
},
{
"id": 2,
"text": "Option 2"
}
]
How can I add that results
key to my JSON?