php - 多维数组转JSON

标签 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。请让我知道如何解决这个问题。提前致谢。

最佳答案

你希望 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 添加到您的查询中。本质上,这会将它们分组在一起,因为该问题的选择的每一行都是相同的。

我在输出中添加了字符串键,我不会混合使用字符串键和编号索引。我希望我正确地绘制了它们。

而且我根本没有测试过这个,如果有任何语法错误,很抱歉。

关于php - 多维数组转JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36819634/

相关文章:

php - 从 php 页面启动在服务器上运行的 php 脚本(并打开套接字)

c# - MYSQL 计数或求和默认值

mysql - 将数据库的表名称与另一个表中的值映射

javascript - 一般使用javascript map函数是什么意思?

javascript - 从下拉列表中单击后 Protractor 不会转到下一行

javascript - 如何将一个很长的 for 循环分解成更小、更易读但仍能执行的部分?

php - mysql 存储旧数据

php - CakePHP 2.4 使用 bcrypt 登录失败

PHP 包含路径问题

mysql - GROUP BY x 与 DISTINCT( x )