簡體   English   中英

JSON的多維數組

[英]Multi-dimensional array to JSON

我正在嘗試打印數據並將其編碼為JSON。 我正在從MySQL數據庫中提取數據以進行測驗。 我正在嘗試提取1個問題,1個類別和4個選項進行一組測驗的數據。 我想我幾乎知道如何使它起作用,但是我沒有找到如何做的。 這是此鏈接中的結果檢查。 將JSON粘貼到此鏈接,以便您輕松設置其格式。 從該JSON數據中,我想為每個問題輸出這樣的內容:

      "What is the approximate number of islands that comprise the Philippines?",
      "Philippine Geography",
      [  
         {  
            "quiz_choice_id":"5",
            "choice":"7000",
            "is_correct_choice":"1"
         },
         {  
            "quiz_choice_id":"6",
            "choice":"6000",
            "is_correct_choice":"0"
         },
         {  
            "quiz_choice_id":"7",
            "choice":"8000",
            "is_correct_choice":"0"
         },
         {  
            "quiz_choice_id":"8",
            "choice":"9000",
            "is_correct_choice":"0"
         }
      ],

這是我的代碼:

 <?php
/**
 * Created by PhpStorm.
 * User: Muhammad
 * Date: 19/04/2016
 * Time: 00:46
 */
require_once('Database_Connect.php');

$questions_query = "SELECT question, quiz_choice_id, choice ,is_correct_choice, category FROM category_question cq, question q, question_choices qc WHERE q.quiz_question_id = qc.quiz_question_id AND q.cat_ques_id = cq.cat_ques_id LIMIT 10";

$questions_query_result = mysqli_query($con, $questions_query) or die("error loading questions");

$questions_results = array();

encode_result($questions_query_result);

function encode_result($result)
{
    //$response = array();
    $questions = array();
    //$choices = array();
    while ($r = mysqli_fetch_array($result)) {
        //$response[] = $r;
        //$questions = array('question' => $r[0]);
        $questions[] = $r['question'];
        $questions[] = $r[4];
        $choices[] = array('quiz_choice_id' => $r[1], 'choice' => $r[2], 'is_correct_choice' => $r[3]);
        $questions[] = $choices;


        //array_push($questions,  $questions['question']);
        //array_push($questions_results, $questions);
    }
    echo json_encode(array('questions'=>$questions));
}

mysqli_close($con);

數據庫的設計是這樣的: 在此處輸入圖片說明

我找不到一種使之工作的方法,因為從數據庫quiz_choice_id,choice, is_correct_choice在另一個表中,但是我將所有表合並到一個表中,正如您在SQL語句$questions_query看到的那樣。 請讓我知道如何解決此問題。 提前致謝。

您是否希望json看起來像這樣?

 [ {
  "question" : "What is the approximate number of islands that comprise the Philippines?",
  "category"    : "Philippine Geography",
  "choices" : [  
         {  
            "quiz_choice_id":"5",
            "choice":"7000",
            "is_correct_choice":"1"
         },
         {  
            "quiz_choice_id":"6",
            "choice":"6000",
            "is_correct_choice":"0"
         },
         {  
            "quiz_choice_id":"7",
            "choice":"8000",
            "is_correct_choice":"0"
         },
         {  
            "quiz_choice_id":"8",
            "choice":"9000",
            "is_correct_choice":"0"
         }
      ]
  },{
    ..... // next question
 }]

您必須執行類似的操作,但是我不知道查詢的結果是什么。

    $questions = array();
    while ($r = mysqli_fetch_array($result)) {
        $key = $r['quiz_question_id']; // you need a key here that is unique to the question, so i think this will do looking at the schema you posted, this will group them in that segment of the array.
        if( !isset( $questions[$key] ) ){
            //if we never had this question, create the top level in the results
            $questions[$key] = array(
                'question' => $r['question'],
                'category'    => $r['category'],
                'choices'  => array()
            );
        }
        //add in the choices for this question
        //on the next iteration, the key is the same so we only do this part
        //and append that rows choices to the previous data using $key to match the question
        $questions[$key]['choices'][] = array('quiz_choice_id' => $r['quiz_choice_id'], 'choice' => $r['choice'], 'is_correct_choice' => $r['is_correct_choice']);
    }

如果這是問題ID(唯一標識符),請確保將quiz_question_id添加到您的查詢中。 本質上,這會將它們分組在一起,因為對於該問題的選擇,每一行都是相同的。

我在輸出中添加了字符串鍵,我不會使用字符串鍵和編號索引來混合使用。 我希望我將它們正確地標出。

另外,我還沒有測試過,如果有語法錯誤,抱歉。

暫無
暫無

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

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