所以我想製作一個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
更新:第三層:
$rows_test2[$r['page']]
['id' => $r['id'],
'type' => $r['type'],
'description' => $r['description']]
[] =
array (
'altid' => $t['altid'],
'altdesc' => $t['altdesc']);
你'$ alternativesarray []',這應該不會是'$ alternativesarray'或'$ alternativesarray [#編號]' – 2012-07-12 08:13:29
「這並沒有真正的工作」 並不真正意味着什麼。它究竟如何不工作? – lanzz 2012-07-12 08:13:54
從數據庫中獲取結果,然後從json_encode中將數組轉換爲Json格式。 http://php.net/manual/en/function.json-encode.php – Vimalnath 2012-07-12 08:15:00