嗨我想查詢最終結果應該看起來像這個json數組下面的關聯數組。然而,我希望數據部分和id名稱部分查詢一次爲每個節點和毗鄰部分多次查詢,如果一個節點有多個鄰接,如果條件是多少,我怎麼查詢此嵌套數組的一部分在保持這種結構的同時,其餘的一次都完成了,但是有多個節點或沒有鄰接節點?從數據庫查詢嵌套關聯數組問題
var json =[{
"adjacencies": [{
"nodeTo": "graphnode9",
"nodeFrom": "graphnode5",
"data": {}
}],
"data": {
"$color": "#C74243",
"$type": "triangle",
},
"id": "graphnode5",
"name": "graphnode5"
}];
這裏是我的數據庫結構
nodes Relationships
----- -------------
id int(11), id int(11),
name varchar(35), to int(11), //this is the destination node from the id relation
color varchar(7), data varchar(0) null
type varchar (12), Foreign key (id) references nodes(id)
Primary key (id)
engine = innodb
這是我在得到一個關聯數組的嘗試,但它查詢一次全部和複製整個strucute。
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);
}
什麼是你的結果數組是什麼樣子?你只會得到一個結果,因爲你不斷重寫數組。你可能的意思是'$ array = array('to'$ array [] = array(' – 2013-03-12 20:37:54
@jcubic lol我幾乎發佈了該編輯。 – 2013-03-12 20:39:13
我的結果數組是這樣的:Array([adjacencies:] => Array([nodeTo] => 2 [nodeFrom] => 1 [data] => Array())[data] => Array([$ color] =>#83548B [$ type] => circle)[id:] => 1 [name :] => Blue Jay),這很好,但如果條件滿足,我想多次查詢鄰接數組中的數組,但仍然保持整體結構,除非有更多的鄰接關係 – user1902588 2013-03-12 20:41:48