多维数组转JSON

Multi-dimensional array to JSON

我正在尝试打印我的数据并将其编码为 JSON。我正在从我的 MySQL 数据库中提取数据以进行测验。我试图提取 1 个问题、1 个类别和 4 个选项来制作一组测验的数据。我想我几乎明白了如何让它发挥作用,但我没能找到方法。这是 link 中的结果检查。 将 JSON 粘贴到此 link 以便您可以轻松地对其进行格式化。从那个 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 处于不同的 table 但我将所有内容合并为一个 table 正如您在中看到的我的 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 添加到您的查询中,这是唯一标识符。从本质上讲,这会将它们组合在一起,因为对于带有该问题选项的每一行,这将是相同的。

我在输出中添加了字符串键,我不会混合使用字符串键和编号索引。我希望我把它们映射对了。

此外,我还没有对此进行任何测试,如果有任何语法错误,请见谅。