我似乎无法正确地将数据导入JSON

时间:2013-05-02 07:49:46

标签: php arrays class json

您好我花了很多时间试图让我的数据在JSON中看起来像这样:

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": []
    },

我正在使用json_encode函数,我知道如何将数据转换为JSON我似乎无法创建任何可以输出上述JSON格式的内容...例如我使用以下代码:

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

这输出:

"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":[

     ]
  }
 ]
}

依旧......但是我无法像上面看到的那样得到孩子们的孩子!

任何帮助都会很棒,谢谢!

编辑:

       class Child{
        public $id;
        public $name;
        public $children;
        public $data;

        public function __construct($id, $sname, $data, $rel){
            $this->children = array();
            $this->data = array();
            if ($rel){
                $this->id = $id+1;
                $this->name = $sname;                   
                $data = array();
                $data["relationTo"] = $rel["name"];
                $data["relation"] = $rel["relation"];
        $this->children[] = new Child($id, $rel["sname"],$data);
            }
            else {
                $this->id = $id+1;
                $this->name = $sname;
                $this->data = $data;
            }
        }       
    }
    $childrel = array();
    $id = 0;
    foreach($relations as $rel){
        $childrel[] = new Child($id ,"","", $rel);
        $id = $id + 1;
    }

1 个答案:

答案 0 :(得分:0)

在foreach循环之前为其分配值之前,您尚未声明$ children。因此,像这样声明数组并将代码更改为此

$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;
    }