Hi Everyone!
Hello I have spent quite bit of time trying to get my data to look like this in JSON as a sample:
var json = {
"id": "347_0",
"name": "Nine Inch Nails",
"children": [{
"id": "126510_1",
"name": "Jerome Dillon",
"data": {
"band": "Nine Inch Nails",
"relation": "member of band"
},
"children": [{
"id": "52163_2",
"name": "Howlin' Maggie",
"data": {
"band": "Jerome Dillon",
"relation": "member of band"
},
"children": []
}, {
"id": "324134_3",
"name": "nearLY",
"data": {
"band": "Jerome Dillon",
"relation": "member of band"
},
"children": []
}]
}, {
"id": "173871_4",
"name": "Charlie Clouser",
"data": {
"band": "Nine Inch Nails",
"relation": "member of band"
},
"children": []
}, {
"id": "235952_5",
"name": "James Woolley",
"data": {
"band": "Nine Inch Nails",
"relation": "member of band"
},
"children": []
},
I am using the json_encode function and I know how to make the data into JSON I just cannot seem to create anything that will output the above JSON format...For example I use the following code:
foreach($relations as $rel){
$data[$id]["relationTo"] = $rel["name"];
$data[$id]["relation"] = $rel["relation"];
$id = $id + 1;
}
$id = 0;
foreach($relations as $rel){
$children[$id]["id"] = $id+1;
$children[$id]["name"] = $rel["sname"];
$children[$id]["data"] = $data[$id];
$id = $id + 1;
}
$relationsArray["id"] = 0;
$relationsArray["name"] = $rel["name"];
$relationsArray["children"] = $children;
$json_content = json_encode($relationsArray);
And this outputs:
"id":0,
"name":"Al",
"children":[
{
"id":1,
"name":"Brandon",
"data":{
"relationTo":"Albaraa",
"relation":"Friend"
},
"children":[
]
},
{
"id":2,
"name":"Shen",
"data":{
"relationTo":"Albaraa",
"relation":"Friend"
},
"children":[
]
},
{
"id":3,
"name":"Dan",
"data":{
"relationTo":"Albaraa",
"relation":"Professor"
},
"children":[
]
},
{
"id":4,
"name":"Bob",
"data":{
"relationTo":"Albaraa",
"relation":"Boss"
},
"children":[
]
},
{
"id":5,
"name":"Al",
"data":{
"relationTo":"Albaraa",
"relation":"God Father"
},
"children":[
]
},
{
"id":6,
"name":"Albaraa",
"data":{
"relationTo":"Shen",
"relation":"Friend"
},
"children":[
]
},
{
"id":7,
"name":"Brandon",
"data":{
"relationTo":"Shen",
"relation":"Friend"
},
"children":[
]
},
{
"id":8,
"name":"Dan",
"data":{
"relationTo":"Shen",
"relation":"Professor"
},
"children":[
]
},
{
"id":9,
"name":"Albaraa",
"data":{
"relationTo":"Al",
"relation":"God Son"
},
"children":[
]
},
{
"id":10,
"name":"Bob",
"data":{
"relationTo":"Al",
"relation":"Best Friends"
},
"children":[
]
}
]
}
and so on...but I am not able to get the children of the children as you see above!
Any help would be amazing thank you!
AVADHESH PATEL
02-May-2013declared $children before assigning values to it before for each loop. Hence declare the array like this and alter the code to this
$children = array();
$id = 0;
foreach($relations as $rel){
$children[$id]["id"] = $id+1;
$children[$id]["name"] = $rel["sname"];
$children[$id]["data"] = $data[$id];
$id = $id + 1;
}
I hope it resolve your problem.