0

嗨,我正在尝试 json_code 一个关联数组,其中最终结果应该看起来像下面的这个 json 数组

   var json =[{  "adjacencies": [
      {
        "nodeTo": "graphnode9",
        "nodeFrom": "graphnode5",
        "data": {}
      }
    ],
    "data": {
      "$color": "#C74243",
      "$type": "triangle"
    },
    "id": "graphnode5",
    "name": "graphnode5"
  }];

这是我尝试将关联数组放入 json_encode 但它似乎不起作用,您可能会看到任何错误吗?太感谢了

   function getjson(){  
   $db = adodbConnect();
   $query = "SELECT nodes.*, relationships.* FROM nodes inner JOIN relationships ON nodes.id = relationships.id";
  $result = $db -> Execute($query);

  while($row=$result->FetchRow())
   {
  $id= (float)$row['id'];
  $name = $row['name'];
  $color1 = $row['color'];
  $type1 = $row['type'];
  $to= (float)$row['to'];

  $array = array(
  "adjacencies:" => array(
  "nodeTo" => "$to",
  "nodeFrom" => "$id",
  "data" => array() ),
  "data" => array(
   "$"."color" => $color1,
   "$"."type" => $type1 ),
  "id".":" => $id,
  "name".":" => $name);

}
print"$array";
json_encode($array);
}
4

1 回答 1

0

您希望您的数组创建看起来像:

$array = array(
  "adjacencies" => array(
      "nodeTo" => "$to",
      "nodeFrom" => "$id",
      "data" => array()
  ),
  "data" => array(
       '$color' => $color1,
       '$type' => $type1
  ),
  'id' => $id,
  'name' => $name
);

这将给出一个结果json_encode,从那里开始,取决于您如何循环以将所有数据返回到您的应用程序。不过,您确实需要分配json_encode给类似的东西$my_json = json_encode($array);

于 2013-03-12T00:51:50.100 回答