2012-07-12 3 views
3

그래서 나를위한 몇 가지 질문에 대한 정보를 담고있는 JSON 개체를 만들려고합니다. 이것은 내가 그것을 제시하는 방법의 의사 코드 :올바른 JSON 형식 가져 오기

{ 
"page" : 1, 
"info" : 
      { 
      "id" : 1, 
      "type" : 3, 
      "description": "How to do JSON?", 
      "alternatives" : 
          { 
          "id" : 1, 
          "description" : "Dunno" 
          } 
          { 
          "id" : 2, 
          "description" : "Let me show you" 
          } 
          { 
          "id" : 3, 
          "description" : "I refuse to show you" 
          } 
      } 
      "id" : 2, 
      "type" : 1, 
      "description": "Do you get it?", 
      "alternatives" : 
          { 
          "id" : 1, 
          "description" : "Yes" 
          } 
          { 
          "id" : 2, 
          "description" : "No" 
          } 
      } 
} 

그래서 코드는 아래 에서 악몽 (답변 중 하나)이며, 나는 페이지와 수행 할 작업을 정확하게 수행 그리고 질문들,하지만 나는 각 질문에 대안들을 연결하는 방법을 알아낼 수 없다. 내가 시도한 부분에서 스 니펫을 볼 수는 있지만 철자가 정확하지 않고 잠시 동안이 부분을 망치고 있습니다.

$before_json_encode[$row['page']][] = array(
    'id' => $row['id'], 
    'type' => $row['type'], 
    'description' => $row['description'], 
    'alternatives' => $alternativesarray//im not sure about here,dont know the structure of the alternative array 
); 

JSON 데이터의 계층 구조를 어떻게 표시할지에 대한 또 다른 그림. 예를 들어 선택할 수 있어야합니다. 특정 페이지의 모든 질문에 대한 모든 대안. 따라서 설문 조사에서 페이지 3을 생성하려면 먼저 3 페이지 하위 배열에서 질문을 찾은 다음 각 질문에서 다시 모든 질문에 액세스 할 수 있습니다. 그 질문 자체의 하위 배열에 연결된 대안이 있습니다. 내 문제의 빈약 한 설명 죄송합니다, 그냥 조금 복잡 :/

Page 
Question 
    Alternative 
    Alternative 
    Alternative 
Question 
    Alternative 
    Alternative 
    Alternative 
Question 
    Alternative 
    Alternative 
Page 
Question 
    Alternative 
    Alternative 
Question 
    Alternative 
    Alternative 

업데이트 : 3 층 :

$rows_test2[$r['page']] 
['id' => $r['id'], 
'type' => $r['type'], 
'description' => $r['description']] 
[] = 
     array (
     'altid' => $t['altid'], 
     'altdesc' => $t['altdesc']); 
+0

당신은'$ alternativesarray []'이'$의 alternativesarray' 또는'$의 alternativesarray [#의 ID 번호] 안' –

+0

"정말 작동하지 않습니다이"정말 아무것도 의미하지 않는다. 얼마나 정확하게 작동하지 않습니까? – lanzz

+0

데이터베이스에서 결과를 가져온 다음 json_encode를 사용하여 배열을 Json 형식으로 변환합니다. http://php.net/manual/en/function.json-encode.php – Vimalnath

답변

3
$rows[] = array(
    "page" => 1, 
    "info" => array(
     "id" => 1, 
     "type" => 3, 
     "description" => 'desc', 
    ) 
); 
echo json_encode($rows); // [{"page":1,"info":{"id":1,"type":3,"description":"desc"}}] 

업데이트 :

$alternativesarray[]=array('id'=>'1', 'description'=>'yes'); 
$alternativesarray[]=array('id'=>'2', 'description'=>'no'); 
$rows[] = array(
    "page" => 1, 
    "info" => array(
     "id" => 2, 
     "type" => 3, 
     "description" => 'desc', 
     "alternatives" => $alternativesarray 
    ) 
); 
print json_encode($rows); // [{"page":1,"info":{"id":2,"type":3,"description":"desc","alternatives":[{"id":"1","description":"yes"},{"id":"2","description":"no"}]}}] 
+0

정적 값을 제외하고는 똑같은 것이 아닙니까? 또는 나는 무엇인가 놓치고 있냐? – Tom

+0

뭔가를 놓치지 않는 한 동일해야합니다. –

+0

그럼 우리 둘 다 틀리다 :) – Tom

1

아마도 이것을 좋아할 것입니까?

$before_json_encode[$row['page']][] = array(
     'id' => $row['id'], 
     'type' => $row['type'], 
     'description' => $row['description'], 
     'alternatives' => $alternativesarray//im not sure about here,dont know the structure of the alternative array 
); 
+0

'description'= $ row [ 'description'] 및 대안은 'id', 'description' 및 '대답'. 거기에 뭔가있을 수도 있습니다. – Tom

+0

죄송합니다. 오류에 대해 :) – Nightmare

+0

당신은 나의 구세주입니다! 완벽하게 작동했습니다! $ alternatives 배열을 채우는 방법을 알아 내야합니다. (여러분) 지금 질문을했습니다. 장엄한! – Tom