【发布时间】:2017-10-04 06:02:18
【问题描述】:
我有this JSON file(缩短版),需要为每个问题循环遍历它#并将问题详细信息添加到 MySQL 数据库。每个测验有 10 个问题。每个测验由一个 URL、标题、图像和问题(数组)组成。每个测验有 10 个问题和 4 个答案选项。
我正在寻找一个起点,这样我就可以开始循环并回答问题。任何帮助将不胜感激。谢谢!
手码数据提取 - 我想循环这个。
// this will be associated with every question in a quiz.
$url = $json['rows'][0]['URL'];
$Title = $json['rows'][0]['Title'];
$Questions = $json['rows'][0]['Questions'];
$Image = $json['rows'][0]['Image'];
// I need these to loop thorugh for Q1, Q2, Q3, etc..... for every Q# in row0
$Q1Text = $json['rows'][0]['Questions']['Question1']['Question Text'];
$Q1MediaURL = $json['rows'][0]['Questions']['Question1']['Question Media']['Media url'];
$Q1MediaType = $json['rows'][0]['Questions']['Question1']['Question Media']['Media Type'];
$Q1Answer1 = $json['rows'][0]['Questions']['Question1']['Answers']['Answer1']['Answer'];
$Q1Answer1Value = $json['rows'][0]['Questions']['Question1']['Answers']['Answer1']['Value'];
$Q1Answer2 = $json['rows'][0]['Questions']['Question1']['Answers']['Answer2']['Answer'];
$Q1Answer2Value = $json['rows'][0]['Questions']['Question1']['Answers']['Answer2']['Value'];
$Q1Answer3 = $json['rows'][0]['Questions']['Question1']['Answers']['Answer3']['Answer'];
$Q1Answer3Value = $json['rows'][0]['Questions']['Question1']['Answers']['Answer3']['Value'];
$Q1Answer4 = $json['rows'][0]['Questions']['Question1']['Answers']['Answer4']['Answer'];
$Q1Answer4Value = $json['rows'][0]['Questions']['Question1']['Answers']['Answer4']['Value'];
$Q2Text = $json['rows'][0]['Questions']['Question2']['Question Text'];
$Q2MediaURL = $json['rows'][0]['Questions']['Question2']['Question Media']['Media url'];
$Q2MediaType = $json['rows'][0]['Questions']['Question2']['Question Media']['Media Type'];
$Q2Answer1 = $json['rows'][0]['Questions']['Question2']['Answers']['Answer1']['Answer'];
$Q2Answer1Value = $json['rows'][0]['Questions']['Question2']['Answers']['Answer1']['Value'];
$Q2Answer2 = $json['rows'][0]['Questions']['Question2']['Answers']['Answer2']['Answer'];
$Q2Answer2Value = $json['rows'][0]['Questions']['Question2']['Answers']['Answer2']['Value'];
$Q2Answer3 = $json['rows'][0]['Questions']['Question2']['Answers']['Answer3']['Answer'];
$Q2Answer3Value = $json['rows'][0]['Questions']['Question2']['Answers']['Answer3']['Value'];
$Q2Answer4 = $json['rows'][0]['Questions']['Question2']['Answers']['Answer4']['Answer'];
$Q2Answer4Value = $json['rows'][0]['Questions']['Question2']['Answers']['Answer4']['Value'];
$Q3Text = $json['rows'][0]['Questions']['Question3']['Question Text'];
$Q3MediaURL = $json['rows'][0]['Questions']['Question3']['Question Media']['Media url'];
$Q3MediaType = $json['rows'][0]['Questions']['Question3']['Question Media']['Media Type'];
$Q3Answer1 = $json['rows'][0]['Questions']['Question3']['Answers']['Answer1']['Answer'];
$Q3Answer1Value = $json['rows'][0]['Questions']['Question3']['Answers']['Answer1']['Value'];
$Q3Answer2 = $json['rows'][0]['Questions']['Question3']['Answers']['Answer2']['Answer'];
$Q3Answer2Value = $json['rows'][0]['Questions']['Question3']['Answers']['Answer2']['Value'];
$Q3Answer3 = $json['rows'][0]['Questions']['Question3']['Answers']['Answer3']['Answer'];
$Q3Answer3Value = $json['rows'][0]['Questions']['Question3']['Answers']['Answer3']['Value'];
$Q3Answer4 = $json['rows'][0]['Questions']['Question3']['Answers']['Answer4']['Answer'];
$Q3Answer4Value = $json['rows'][0]['Questions']['Question3']['Answers']['Answer4']['Value'];
更新:这是我最终得到的工作代码。感谢@Lou 的帮助!
foreach($json['rows'] as $row){ // This will loop trough every rows in your array.
foreach($row['Questions'] as $question){ //This will loop trough all the questions of the current row.
echo ''.print_r($question).'<br>'; //You can access the array of your question here. using $question. For now it will just print it's content.
echo "<strong>Quiz URL:</strong> " . $row["URL"]. "<br>";
echo "<strong>Quiz Title:</strong> " . $row["Title"]. "<br>";
echo "<strong>Quiz Image:</strong> " . $row["Image"]. "<br><br><br>";
echo "<strong>Question Text:</strong> " . $question['Question Text']. "<br>";
echo "<strong>Media URL:</strong> " . $question['Question Media']['Media url']. "<br>";
echo "<strong>Media Type:</strong> " . $question['Question Media']['Media Type']. "<br>";
echo "<strong>Answer1:</strong> " . $question['Answers']['Answer1']['Answer'] . " | " . $question['Answers']['Answer1']['Value'] . "<br>";
echo "<strong>Answer2:</strong> " . $question['Answers']['Answer2']['Answer'] . " | " . $question['Answers']['Answer2']['Value'] . "<br>";
echo "<strong>Answer3:</strong> " . $question['Answers']['Answer3']['Answer'] . " | " . $question['Answers']['Answer3']['Value'] . "<br>";
echo "<strong>Answer4:</strong> " . $question['Answers']['Answer4']['Answer'] . " | " . $question['Answers']['Answer4']['Value'] . "<br>";
echo "<br>";
}
}
【问题讨论】:
-
欢迎来到 StackOverflow!如果您遇到特定的编程问题,我们很乐意为您提供帮助,但我们不是来为您编写代码的。请参阅How do I ask a good question? 和What topics can I ask about here?。
-
@AlexHowansky 感谢您查看我的问题并做出回应。我知道 SO 不是来这里写代码的,我也不是在这里寻找复制/粘贴代码。我只是为我的循环寻找一个“起点”。我已经编写了上面的代码,我正在寻找将其转换为每个问题的循环的指导。非常感谢任何帮助。
标签: php arrays json loops foreach