使用php mysql创建嵌套的json对象

前端之家收集整理的这篇文章主要介绍了使用php mysql创建嵌套的json对象前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两个表,表1有2个字段(question_pk,question_name),表2有4个字段(ans_pk,options,question_fk和right_answer).我想像下面的结构一样创建json
{
    "type": "quiz","name": "Brand Colors","description": "Can you identify these brands by the background color?","questions": [
        {
            "name": "Can you identify this color?","description": "#ea4c89","answers": [
                {
                    "name": "Dribbble","description": "dribbble.png","weight": 1
                },{
                    "name": "Amazon","description": "amazon.png","weight": 0
                },{
                    "name": "Apple","description": "apple.png","weight": 0
                }
            ]
        },{
            "name": "Can you identify this color?","description": "#3d9ae8","answers": [
                {
                    "name": "Youtube","description": "youtube.png",{
                    "name": "DropBox","description": "dropBox.png",{
                    "name": "wordpress","description": "wordpress.png","description": "#c4302b",{
                    "name": "Twitter","description": "twitter.png",{
                    "name": "Vimeo","description": "vimeo.png","weight": 0
                }
            ]
        }

    ]
}

我的PHP代码

<?PHP
include '../config/config.PHP';
if(isset($_GET['sub_cat_id']))
{
         $sub_cat_id = $_GET['sub_cat_id']; 
        $result = MysqL_query("select * from $questions where sub_cat='$sub_cat_id' order by level_fk asc"); 
        $json_response = array(); //Create an array
        $i=1;
                        while ($row = MysqL_fetch_array($result))
                        {
                        $row_array['qus_pk'] = $row['qus_pk'];        
                        $row_array['question'] = $row['question'];
                        $qus_pk = $row['qus_pk'];  


                        $option_qry = MysqL_query("select * from $qus_ans where qus_pk=$qus_pk");
                        while ($opt_fet = MysqL_fetch_array($option_qry))
                        {

                        $row_array['options'] = $opt_fet['options'];  
                        $row_array['right_ans'] = $opt_fet['right_ans'];  
                        array_push($json_response,$row_array); //push the values in the array
                            }                       


                        $i++;
                        }
        echo json_encode($json_response);
}

?>

而我的结果我得到的json响应如下

[
    {
        "qus_pk": "1","question": "Ten years ago,P was half of Q in age. If the ratio of their present ages is 3:4,what will be the total of their present ages?","options": "45","right_ans": "0"
    },{
        "qus_pk": "1","options": "40","options": "35","right_ans": "1"
    },"options": "50",{
        "qus_pk": "2","question": "Father is aged three times more than his son Sunil. After 8 years,he would be two and a half times of Sunil's age. After further 8 years,how many times would he be of Sunil's age?","options": "4 times","options": "1 times","options": "3 times","options": "5 times","right_ans": "0"
    }
]

在每次重复问题时,如何避免,如果我想要实现第一个json结构,在我的PHP代码中我需要做什么以及在哪里进行更改?如果有人知道帮助我.

嗨试试这个,
<?PHP
include '../config/config.PHP';
if(isset($_GET['sub_cat_id']))
{
    $sub_cat_id = $_GET['sub_cat_id']; 
    $result = MysqL_query("SELECT * FROM $questions WHERE sub_cat='$sub_cat_id' ORDER BY level_fk ASC"); 
    $json_response = array(); //Create an array
    while ($row = MysqL_fetch_array($result))
    {
        $row_array = array();
        $row_array['qus_pk'] = $row['qus_pk'];        
        $row_array['question'] = $row['question'];
        $row_array['answers'] = array();
        $qus_pk = $row['qus_pk'];  

        $option_qry = MysqL_query("SELECT * FROM $qus_ans WHERE qus_pk=$qus_pk");
        while ($opt_fet = MysqL_fetch_array($option_qry))
        {
            $row_array['answers'][] = array(
                'options' => $opt_fet['options'],'right_ans' => $opt_fet['right_ans'],);

        }
        array_push($json_response,$row_array); //push the values in the array
    }
    echo json_encode($json_response);
}
?>

猜你在找的PHP相关文章