dsg435665475 2017-03-31 10:26
浏览 61

使用php mysql显示多维数组的JSON代码

I am trying to display multidimensional array in JSON using php and mysql.How to display an multidimensional array in JSON. I am new to JSON code.. I want my JSON code to display in this way

{"staffdirectory": [
        {
            "teachingstaff":[
                       {
                           "deptA":[
                               {
                                  "name":"xxxxxxxxx",
                                  "email":"xxxxxxxx",
                                  "designation":"xxxxxxx"
                                }]
                       },      

                       {
                           "deptB":[
                                {
                                  "name":"xxxxxxxxx",
                                  "email":"xxxxxxxx",
                                  "designation":"xxxxxxx"
                                }
                                ]
                                { 
                                  "name":"xxxxxxxxx",
                                  "email":"xxxxxxxx",
                                  "designation":"xxxxxxx"
                                }
                                ]
                       },
                       ]
        },
        {
            "non-teachingstaff":[
                       {
                           "principalA":[
                               {
                                  "name":"xxxxxxxxx",
                                  "email":"xxxxxxxx",
                                  "designation":"xxxxxxx"
                                }
                                ]
                       },      

                       {
                           "principalB":[
                                {
                                  "name":"xxxxxxxxx",
                                  "email":"xxxxxxxx",
                                  "designation":"xxxxxxx"
                                }
                                ]
                       },
 ]
        }
    ]
    }

I need to create an array for staff which contains teaching and non teaching in again in staff i need to create different departments and again an array to lecturers In each department..

Here is the code

<?php
include_once "db.php";

    if(isset($_POST['updated_date'])) {
        // get all products from products table
        $result = mysql_query("select lecturers1.lname,department1.name,staff_directory1.role
 from lecturers1
  inner join department1 on lecturers1.dept_key=department1.key
  inner join staff_directory1 on lecturers1.sd_key=staff_directory1.key") or die(mysql_error());
    }else{
        // get all products from products table
        $result = mysql_query("select lecturers1.lname,department1.name,staff_directory1.role
 from lecturers1
  inner join department1 on lecturers1.dept_key=department1.key
  inner join staff_directory1 on lecturers1.sd_key=staff_directory1.key") or die(mysql_error());

    }

    // check for empty result
    if (mysql_num_rows($result) > 0) {
        // looping through all results items node
        $response["staff"] = array();
        while ($row = mysql_fetch_array($result)) {
            // temp user array
            $news = array();

            $news["role"]=$row["role"];
              $dept = array();

            $dept["name"]=$row["name"];
            $lecturer1=array();
            $lecturer1["lname"]=$row["lname"];
            $news['lecturer1']=json_encode($lecturer1);
            $news['dept']=json_encode($dept);
        array_push($response["staff"], $news);

            // push single product into final response array

        }

        // success
        $response["success"] = 1;
        $response["message"] = mysql_num_rows($result)." items found";
        // echoing JSON response
        echo json_encode($response);
    } else {
        // no products found
        $response["success"] = 0;
        $response["message"] = "No newsitems found";
        // echo no users JSON
        echo json_encode($response);
    }
   mysql_close($conn);
?>

My JSON code displays this way

"staff": [
  {
"role": "Teaching",
"lecturer1": "{"lname":"c1"}",
"dept": "{"name":"computer science"}"
},
  {
"role": "Teaching",
"lecturer1": "{"lname":"c2"}",
"dept": "{"name":"computer science"}"
},
  {
"role": "Teaching",
"lecturer1": "{"lname":"e1"}",
"dept": "{"name":"english"}"
},
  • 写回答

1条回答 默认 最新

  • duan0417 2017-03-31 10:40
    关注

    Use json_decode() to convert JSon data to multi dimension array. When TRUE, returned objects will be converted into associative arrays.

    print_r(json_decode(json_array, true));

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?