2013-03-12 5 views
0

안녕하세요, 최종 배열 결과가이 json 배열처럼 보이는 연관 배열을 쿼리하려고합니다. 그러나 데이터 섹션과 ID 이름 섹션을 한 번만 쿼리하면 모든 노드와 인접성 섹션에 여러 번 쿼리 할 수 ​​있습니다. 노드에 여러 개의 인접성이있는 경우 조건이 충족되면이 중첩 된 배열의 한 섹션을 한 번 이상 쿼리하는 방법 이 struture를 유지하면서 노드에 대한 다중 또는 없음 인접성을 유지하면서 한 번 만난 적이 있습니까?중첩 된 연관 배열에서 데이터베이스 문제 쿼리

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 중복입니다. 당신은 루프 외부에서 배열을 생성하고

$array = array(); 
while($row=$result->FetchRow()) { 
    $array[] = array(
     "adjacencies:" => array(
      "nodeTo" => (float)$row['to'], 
      "nodeFrom" => (float)$row['id'], 
      "data" => array() 
     ), 
     "data" => array(
      '$color' => $row['color'], 
      '$type' => $row['type'] 
     ), 
     "id" => (float)$row['id'], 
     "name" => $row['name'] 
    ); 

} 
+0

무엇 당신의 결과 배열이 생겼에 항목을 추가 할 필요가

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

+0

@jcubic lol을 편집하려고했습니다. –

+0

결과 배열은 다음과 같습니다 : Array ([adjacencies :] => Array ([nodeTo] => 2 [nodeFrom] => 1 [data] => Array()) [데이터] => 배열 ([$ color] => # 83548B [$ type] => circle) [id :] => 1 [name :] => Blue Jay) 괜찮 았지만 조건을 충족 시키더라도 adjacencies 내의 배열에 두 번 이상 쿼리하려고하지만 더 많은 인접성을 제외하고는 전체 구조를 그대로 유지합니다 – user1902588

답변

1

? 배열을 다시 작성하기 때문에 하나의 결과 만 얻습니다. 당신은 아마도`$ array = array (`$ array [] to array (``$ array [] to array)``
+0

어떻게 항목을 추가합니까? 그런 다음 구체적으로 그 배열을 테스트해야합니까? – user1902588

+0

@ user1902588이'$ array [] ='요청한 경우 배열에 요소를 추가하십시오. – jcubic