this is my code:
$db = connect_mysqli();
$response = array();
$sql = "SELECT * FROM questions ORDER BY RAND ()";
$result = $db->query($sql);
while($row = $result->fetch_array(MYSQL_ASSOC))
{
$response['answers'][$row['id']] = array('question_id'=>$row['id'], 'option_id'=>null);
}
echo json_encode($response);
And this is the response:
{
answers: {
1: {
question_id: "1",
option_id: null
},
2: {
question_id: "2",
option_id: null
},
3: {
question_id: "3",
option_id: null
},
4: {
question_id: "4",
option_id: null
}
}
}
How to make the response always return JSON Array? not JSON Object like that. Sometimes the response is return JSON Array and in some part, return JSON Object. I want all response tobe JSON Array.
So, it should be like this:
{
answers: [
1: {
question_id: "1",
option_id: null
},
2: {
question_id: "2",
option_id: null
},
3: {
question_id: "3",
option_id: null
},
4: {
question_id: "4",
option_id: null
}
]
}
$response['answers'][$row['id']]to$response['answers'][]