我在输出 json 之前无法合并数据。目标是让 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"
}
这是可以实现的吗?如果是这样,任何让我走上正确道路的建议将不胜感激!
提前致谢!