0

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 Answer 1

1

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));

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.