多维数组到JSON

时间:2016-04-24 05:30:31

标签: php mysql arrays 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);

数据库的设计是这样的: enter image description here

我找不到让它工作的方法,因为数据库quiz_choice_id,choice, is_correct_choice位于不同的表中,但我将所有内容合并到一个表中,就像我在SQL语句$questions_query中看到的那样。请让我知道如何解决这个问题。提前谢谢。

1 个答案:

答案 0 :(得分:1)

你想让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。基本上这会将它们组合在一起,因为对于每个问题的选择,这将是相同的。

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

此外,我还没有对此进行过测试,很抱歉,如果有任何语法错误。