doujubeng2942 2017-05-05 17:01
浏览 46
已采纳

我需要帮助循环使用PHP的JSON文件

I have this JSON file (shortened version) and need to loop through it for each question# and add the question details to a MySQL DB. There are 10 questions per quiz. Each quiz consists of a url, title, image, and questions(array). There are 10 questions with 4 answer options for each quiz.

I'm looking for a starting point so I can begin to loop through and echo the questions. Any help will be greatly appreciated. Thanks!

Hand Code Data Extraction - I want to loop through this.

// 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'];

UPDATE: This is the working code I ended up with. Thanks for your help @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>";
    }
} 
  • 写回答

1条回答 默认 最新

  • duanjizi9443 2017-05-05 17:17
    关注

    So I assume, by looking at your current code, that you already decoded your json as an array.

    Now if you want to loop trough that array. You may want to use foreach loops. See the snippet below if it can help you :

    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 '<pre>'.print_r($question).'</pre><br><br>'; //You can access the array of your question here. using $question. For now it will just print it's content.
        }
    }
    

    If you need further help, let me know.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题