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"}"
},