1
我在寫一個PHP腳本,可以以下面的格式返回一個JSON文件。我想通過從我的數據庫表中獲取數據來創建這個結構。我正在使用SlickQuiz插件,並且很難在問題對象內部創建一個對象數組(即選項),該對象數組又被作爲數組包含在內。在PHP中返回複雜的JSON結構
{
"info": {
"name": "This is Exam name",
"main": "Find out with this super crazy knowledge",
"results": "Get ready",
"level1": "Result Poor",
"level2": "Result Average",
"level3": "Result Good",
"level4": "Result Very Good",
"level5": "Result Great"
},
"questions": [
"q": "Which is the letter A in the English alphabet?",
"a": [
{"option": "8", "correct": false},
{"option": "14", "correct": false},
{"option": "1", "correct": true},
{"option": "23", "correct": false}
],
"correct": "This is correct",
"incorrect": "It's the first letter of the alphabet."
},
{
"q": "Eureka Which of the following best represents your preferred breakfast?",
"a": [
{"option": "Bacon and eggs", "correct": false},
{"option": "Fruit, oatmeal, and yogurt", "correct": true},
{"option": "Leftover pizza", "correct": false},
{"option": "Eggs, fruit, toast, and milk", "correct": true}
],
"select_any": true,
"correct": "<p><span>Nice!</span> Your cholestoral level is probably doing alright.</p>",
"incorrect": "<p><span>Hmmm.</span> You might want to reconsider your options.</p>"
},
{
"q": "Eureka Where are you right now? Select ALL that apply.",
"a": [
{"option": "Planet Earth", "correct": true},
{"option": "Pluto", "correct": false},
{"option": "At a computing device", "correct": true},
{"option": "The Milky Way", "correct": true}
],
"correct": "<p><span>Brilliant!</span> You're seriously a genius, (wo)man.</p>",
"incorrect": "<p><span>Not Quite.</span> You're actually on Planet Earth, in The Milky Way, At a computer. But nice try.</p>"
},
{
"q": "How many Eureka of rain does Michigan get on average per year?",
"a": [
{"option": "149", "correct": false},
{"option": "32", "correct": true},
{"option": "3", "correct": false},
{"option": "1291", "correct": false}
],
"correct": "<p><span>Eureka bananas!</span> I didn't actually expect you to know that! Correct!</p>",
"incorrect": "<p><span>Fail.</span> Sorry. You lose. It actually rains approximately 32 inches a year in Michigan.</p>"
},
{
"q": "Is Earth bigger than a basketball?",
"a": [
{"option": "Yes", "correct": true},
{"option": "No", "correct": false}
],
"correct": "<p><span>Eureka Job!</span> You must be very observant!</p>",
"incorrect": "<p><span>ERRRR!</span> What planet Earth are <em>you</em> living on?!?</p>"
]
}
下面是我試圖使用PHP生成這個。
function generateJSON($pdo){
$response = array();
$response["error"] = false;
$response["questions"] = array();
$stmt = $pdo->prepare("SELECT * FROM questions");
$stmt->execute();
$result= $stmt->fetchAll();
if($stmt->rowCount() > 0){
foreach($result as $row) {
$tmp = array();
$tmp["id"] = $row["id"];
$tmp["q"] = $row["question"];
$tmp["correct"] = $row["question"];
$tmp["incorrect"] = $row["subject_id"];
$tmp["status"] = $row["level_id"];
//Fetching the options
$stmt2 = $pdo->prepare("SELECT * FROM question_options WHERE question_id = ".$tmp["id"]);
$stmt2->execute();
$opt_result= $stmt2->fetchAll();
foreach($opt_result as $opt_row) {
$option = array();
$option["option"] = $opt_row["option_text"];
$option["correct"] = $opt_row["is_correct"] ==1;
array_push($response["questions"], $option);
}
//End of fetching options for this question
array_push($response["questions"], $tmp);
}
}
echoRespnse(200, $response);
}
我有些新來這...不知道如果我需要創建一個類似的PHP類,然後將其編碼爲json。但任何示例或建議都非常感謝。 –
創建一組實現'JsonSerializable'接口並在複雜對象上使用'json_encode'的類是面向對象的方法來完成這一任務,但是是可選的。製作一個封裝你需要的所有信息的關聯數組可能更簡單。 – apokryfos