php 复杂 json 生成,在PHP中返回复杂的JSON结构

我正在编写一个PHP脚本,可以以下面的格式返回给我一个JSON文件。我想通过从我的数据库表中获取数据来创建这个结构。我正在使用SlickQuiz插件,并且很难在问题对象内部创建一个对象数组(即选项),该数组又被重新包含在一个数组中。

{

"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": "

Nice! Your cholestoral level is probably doing alright.

",

"incorrect": "

Hmmm. You might want to reconsider your options.

"

},

{

"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": "

Brilliant! You're seriously a genius, (wo)man.

",

"incorrect": "

Not Quite. You're actually on Planet Earth, in The Milky Way, At a computer. But nice try.

"

},

{

"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": "

Eureka bananas! I didn't actually expect you to know that! Correct!

",

"incorrect": "

Fail. Sorry. You lose. It actually rains approximately 32 inches a year in Michigan.

"

},

{

"q": "Is Earth bigger than a basketball?",

"a": [

{"option": "Yes", "correct": true},

{"option": "No", "correct": false}

],

"correct": "

Eureka Job! You must be very observant!

",

"incorrect": "

ERRRR! What planet Earth are you living on?!?

"

]

}以下是我如何使用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);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值