我在輸出json之前合併數據時遇到問題。目標是讓json中的每個對象都包含每個'post'的所有數據。Json的合併行編碼
下面的代碼:
$results = mysql_query("SELECT dfuw_posts.ID, dfuw_posts.post_title,
dfuw_postmeta.meta_key, dfuw_postmeta.post_id, dfuw_postmeta.meta_value FROM
dfuw_posts INNER JOIN dfuw_postmeta ON dfuw_posts.ID = dfuw_postmeta.post_id WHERE
dfuw_posts.post_type = 'poi' AND dfuw_posts.post_status = 'publish' AND
dfuw_postmeta.meta_key IN ('_poi_loc' ,'_poi_level' ,'_poi_type')");
$pois = array();
while($r = mysql_fetch_assoc($results)){
$pois[] = $r;
}
echo json_encode($pois);
輸出:
[
{
"ID": "320",
"post_title": "Bank",
"meta_key": "_poi_loc",
"post_id": "320",
"meta_value": "70.4845662217412|-84.5123291015625"
},
{
"ID": "320",
"post_title": "Bank",
"meta_key": "_poi_level",
"post_id": "320",
"meta_value": "easy"
},
{
"ID": "320",
"post_title": "Bank",
"meta_key": "_poi_type",
"post_id": "320",
"meta_value": "bank"
},
{
"ID": "324",
"post_title": "A Creature",
"meta_key": "_poi_type",
"post_id": "324",
"meta_value": "mob"
},
{
"ID": "324",
"post_title": "A Creature",
"meta_key": "_poi_level",
"post_id": "324",
"meta_value": "med"
},
{
"ID": "324",
"post_title": "A Creature",
"meta_key": "_poi_loc",
"post_id": "324",
"meta_value": "70.4845662217412|-84.5123291015625"
}
]
我想對於每一個職位進行捆綁,如:
{
"title": "A Creature",
"location": "70.4845662217412|-84.5123291015625",
"type": "mob",
"level": "hard"
}
這是可以實現的?如果是這樣,任何建議送我在正確的道路將不勝感激!
在此先感謝!
檢查了這個 - http://stackoverflow.com/questions/13156159/json-menu-structure-from-mysql/13157562#13157562 – Joddy
@Joddy,謝謝!將研究這一點。這看起來像我想要實現的。一旦我離開並睡一會兒,我會試試。 – nmynarcik