I have MySQL table named qbank having fields - qid, question_text, subject_id, option_01_text, option_01_is_correct, option_02_text, option_02_is_correct, option_03_text, option_03_is_correct, option_04_text, option_04_is_correct.
My desired JSON output is like this -
[{
"qid": 5788,
"question_text": "Which of the following is a traction epiphysis ?",
"subject_id": 2,
"options": [{
"option_code": "A",
"option_text": "Tibial condyles",
"option_is_correct": 0
},
{
"option_code": "B",
"option_text": "Trochanter of femur",
"option_is_correct": 1
},
{
"option_code": "C",
"option_text": "Coracoid process of scapula",
"option_is_correct": 0
},
{
"option_code": "D",
"option_text": "head of femur",
"option_is_correct": 0
}
]
},
{
"qid": 5789,
"question_text": "Which is the most prominent spinous process",
"subject_id": 2,
"options": [{
"option_code": "A",
"option_text": "T1",
"option_is_correct": 0
},
{
"option_code": "B",
"option_text": "C7",
"option_is_correct": 1
},
{
"option_code": "C",
"option_text": "C6",
"option_is_correct": 0
},
{
"option_code": "D",
"option_text": "L5",
"option_is_correct": 0
}
]
}
]
My PHP code is -
$sql = "SELECT * FROM qbank Limit 2";
$query = $mysqli->prepare($sql);
$query->execute();
$result = $query->get_result();
$output = [];
$options = [];
while ($row = $result->fetch_assoc()) {
$output[] = [
"qid" => $row["qid"],
"question_text" => $row["question_text"],
"subject_id" => $row["subject_id"],
];
$options[] = [
"option_code" => "A",
"option_text" => $row["option_01_text"],
"option_is_correct" => $row["option_01_is_correct"],
];
$options[] = [
"option_code" => "B",
"option_text" => $row["option_02_text"],
"option_is_correct" => $row["option_02_is_correct"],
];
$options[] = [
"option_code" => "C",
"option_text" => $row["option_03_text"],
"option_is_correct" => $row["option_03_is_correct"],
];
$options[] = [
"option_code" => "D",
"option_text" => $row["option_04_text"],
"option_is_correct" => $row["option_04_is_correct"],
];
}
echo json_encode($output, JSON_PRETTY_PRINT);
echo json_encode($options, JSON_PRETTY_PRINT);
The output I am getting from PHP is -
[
{
"qid": 5788,
"question_text": "Which of the following is a traction epiphysis ?",
"subject_id": 2
},
{
"qid": 5789,
"question_text": "Which is the most prominent spinous process",
"subject_id": 2
}
][
{
"option_code": "A",
"option_text": "Tibial condyles",
"option_is_correct": 0
},
{
"option_code": "B",
"option_text": "Trochanter of femur",
"option_is_correct": 1
},
{
"option_code": "C",
"option_text": "Coracoid process of scapula",
"option_is_correct": 0
},
{
"option_code": "D",
"option_text": "head of femur",
"option_is_correct": 0
},
{
"option_code": "A",
"option_text": "T1",
"option_is_correct": 0
},
{
"option_code": "B",
"option_text": "C7",
"option_is_correct": 1
},
{
"option_code": "C",
"option_text": "C6",
"option_is_correct": 0
},
{
"option_code": "D",
"option_text": "L5",
"option_is_correct": 0
}
]
My approach was to somehow push $options inside $output, but even after several tries, I am not able to find the correct way.