【问题标题】:Create nested json using php mysql使用 php mysql 创建嵌套 json
【发布时间】:2017-07-18 01:58:35
【问题描述】:

我有以下三个表

  1. 讲座(lec_id、名称、描述)
  2. 测试(test_id、test_name、lec_id、日期)
  3. 问题(q_id、q_name、q_desc、test_id)

我想生成这样的 json 响应

{
"lec_name": "Math",
"description": "Can you identify these brands by the background color?",
"test": [
    {
        "name": "Algebra",
        "date": "10-6-2017",
        "question": [
            {
                "q_name": "question 1",
                "description": "Lorem Ipsum is simply dummy text of the printing",
            },
            {
                "q_name": "question 2",
                "description": "Lorem Ipsum is simply dummy text of the printing",
            },
            {
                "q_name": "question 3",
                "description": "Lorem Ipsum is simply dummy text of the printing",
            }
        ]
    }


] }

但我越来越喜欢这个了

[
    [
        {
            "algebra": "2017-02-28"
        }
    ],
    {
        "question 1": "Lorem Ipsum is simply dummy text of the printing"
    },
    {
        "0": "Math",
        "1": "1",
        "name": "Math",
        "lec_id": "1"
    },
    [
        {
            "trigonometry": "2017-02-28"
        }
    ],
    {
        "question 2": "Lorem Ipsum is simply dummy text of the printing"
    },
    {
        "0": "Chemistry",
        "1": "2",
        "name": "Chemistry",
        "lec_id": "2"
    },
    [
        {
            "Bio test 1": "2017-02-26"
        }
    ],
    {
        "question 3": "Lorem Ipsum is simply dummy text of the printing"
    },
    {
        "0": "Physics",
        "1": "3",
        "name": "Physics",
        "lec_id": "3"
    },
    [
        {
            "Bio test 2": "2017-02-28"
        }
    ],
    {
        "question 4": "Lorem Ipsum is simply dummy text of the printing"
    },
    {
        "0": "Biology",
        "1": "4",
        "name": "Biology",
        "lec_id": "4"
    }
]

这是我的代码,

    $sql = "SELECT name, lec_id FROM lecture";

$sqlRun = mysqli_query($conn , $sql);
//var_dump($sqlRun);
//echo $sqlRun;     
$json = array();
$total_records = mysqli_num_rows($sqlRun);

if($total_records > 0){
    while($row = mysqli_fetch_array($sqlRun)){
        $row_array= array();


        $qus_pk = $row['lec_id'];
        $lec_desc = '';
        $lec_name = '';

        $option_qry = mysqli_query($conn, "SELECT test_name, date, test_id FROM test WHERE test_id= $qus_pk");
        //$option_qry = mysqli_query($conn, "SELECT t.name");
        while($opt_fet = mysqli_fetch_array($option_qry)){
            $row_array[]= array(
                $opt_fet['test_name'] => $opt_fet['date'],

            );
            $quest_array = array();
            $quest_pk = $opt_fet['test_id'];
            $test_query = mysqli_query($conn, "SELECT q_name, q_desc FROM question WHERE q_id = $quest_pk");
            while($test_fet = mysqli_fetch_array($test_query)){
                $quest_array= array(
                    $test_fet['q_name'] => $test_fet['q_desc'],
                );
            }
        }
        array_push($json, $row_array, $quest_array);
        $json[] = $row;
    }
}


echo json_encode($json);

【问题讨论】:

    标签: php json mysqli


    【解决方案1】:

    以此更改您的代码。

    if($total_records > 0){
        $i = 0;
        while($row = mysqli_fetch_array($sqlRun)){
            $row_array= array();
            $qus_pk = $row['lec_id'];
            $json[$i]['lec_name'] = $row['name'];    
            $json[$i]['description'] = $row['description'];    
    
            $option_qry = mysqli_query($conn, "SELECT test_name, date, test_id FROM test WHERE test_id= $qus_pk");
            //$option_qry = mysqli_query($conn, "SELECT t.name");
            while($opt_fet = mysqli_fetch_array($option_qry)){
                $json[$i]['test']['name'] = $opt_fet['name'];
                $json[$i]['test']['date'] = $opt_fet['date'];
    
                $quest_array = array();
                $quest_pk = $opt_fet['test_id'];
                $test_query = mysqli_query($conn, "SELECT q_name, q_desc FROM question WHERE q_id = $quest_pk");
                $j = 0;
                while($test_fet = mysqli_fetch_array($test_query)){
                    $json[$i]['test']['question'][$j] = array('q_name' => $test_fet['q_name'], 'description' => $test_fet['q_desc']);
                    $j++;
                }
            }
            $i++;
        }
    }
    
    
    echo json_encode($json);
    

    【讨论】:

    • #naincy 感谢您的回答,我在 while 循环中遇到错误,它的“解析错误:语法错误,意外 '['” while($test_fet = mysqli_fetch_array($test_query)){ $json [$i]['test']['question'][] = ['q_name' => $test_fet['q_name'], 'description' => $test_fet['q_desc']]; }
    • @BilalCh 它不应该给出错误...... :(你能确保你复制粘贴正确......这个错误在哪一行和那一行写的你可以分享跨度>
    • #naincy 请看一下这些屏幕截图prnt.sc/edytlnprnt.sc/edytrv
    • 做或不做。没有“尝试”。 好的答案将始终解释所做的事情以及为什么以这种方式完成,不仅是为了 OP,也是为了 SO 的未来访问者。跨度>
    • @BilalCh 是第 33 行吗?
    猜你喜欢
    • 2018-09-05
    • 2019-01-10
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 2015-06-05
    • 2019-03-02
    • 2016-01-24
    相关资源
    最近更新 更多