dies ist mein Code:Wie macht man json_encode immer als JSON Array zurück?
$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);
Und das ist die Antwort:
{
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
}
}
}
Wie die Antwort immer wieder zurückkehren JSON Array machen? nicht JSON Objekt wie das. Manchmal gibt die Antwort JSON Array zurück und gibt JSON Object zurück. Ich möchte alle Antwort auf JSON Array.
So soll es so sein:
$response['answers'][] = array('question_id'=>$row['id'], 'option_id'=>null);
Das Problem:
{
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
}
]
}
Try Änderung '$ response [ 'Antworten'] [$ row [ 'id']]' auf '$ response [ 'Antworten'] []' –
ich brauche dies als Schlüssel $ response [ 'Antworten '] [$ row [' id ']] @MrHery –
Dann versuchen, aus der Datenbank als ein Objekt, nicht als Array abrufen. –