【发布时间】:2018-09-08 23:28:51
【问题描述】:
我有一个来自 php 的 json 数据,它运行良好。 我现在想要的是,我希望我的 json 数据像下面的 javascript 函数中的代码一样。 我想迭代并获得类似的数据,如 javascript 代码中的对象。 我如何获得它。
$json_array = array();
$sql = "SELECT id, instructions, quiz_question, correct, wrong, wrong1, wrong2 FROM student_quiz WHERE subject = 'SOCIAL STUDIES' AND type = 'challenge'";
$results = $pdo->query($sql);
$results->setFetchMode(PDO::FETCH_ASSOC);
while($row = $results->fetch()) {
$json_array['quizlist'][] = $row ;
}
?>
<script type='text/javascript'>
(function() {
var questions = [{
question: "What is 3*6?",
choices: [3, 6, 9, 12, 18],
correctAnswer: 4
}, {
question: "What is 8*9?",
choices: [72, 99, 108, 134, 156],
correctAnswer: 0
}, {
question: "What is 1*7?",
choices: [4, 5, 6, 7, 8],
correctAnswer: 3
}, {
question: "What is 8*8?",
choices: [20, 30, 40, 50, 64],
correctAnswer: 4
}];
【问题讨论】:
-
你看
json_encode函数了吗?
标签: javascript php mysql json