簡體   English   中英

我需要使用php循環遍歷JSON文件的幫助

[英]I need help looping through a JSON file with php

我有這個JSON文件 (縮短版本),需要遍歷每個問題#,並將問題詳細信息添加到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>";
    }
} 

因此,我認為,通過查看當前代碼,您已經將json解碼為數組。

現在,如果要循環遍歷該數組。 您可能要使用foreach循環。 如果可以幫助您,請參見下面的代碼段:

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.
    }
}

如果您需要進一步的幫助,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM