0

我在输出 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"
 }

这是可以实现的吗?如果是这样,任何让我走上正确道路的建议将不胜感激!

提前致谢!

4

2 回答 2

0

在 mysql 查询中提供自定义标题将解决您的问题。

$results = mysql_query("
         SELECT 
            dfuw_posts.ID,
            dfuw_posts.post_title AS title,
            dfuw_postmeta.meta_key,
            dfuw_postmeta.post_id,
            dfuw_postmeta.meta_value AS location
         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);

尝试并发布您最终的结果。

于 2013-01-03T11:46:22.387 回答
0

以下是我最终解决问题的方法。

$results = mysql_query("
     SELECT
        dfuw_posts.ID AS pid,
        dfuw_posts.post_title AS title,
        dfuw_postmeta.meta_key AS mkey,
        dfuw_postmeta.post_id AS pmid,
        dfuw_postmeta.meta_value AS mval
     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')");

  $jsary = ["pois" => []];
  $lastPid = 0;
  $currentPid = 0;
  $title = "";
  $ifff = 0;
  $elss = 0;
    while($row = mysql_fetch_array($results))
    {
        $currentPid = $row['pid'];
        $title = $row['title'];

        $cmd = $row['mkey'];
        $cmt = $row['mval'];

        if($lastPid != $currentPid)
        {
                $insAry = [];
                $insAry = ["title"=> $title, $cmd => $cmt];
                array_push($jsary["pois"], $insAry);
                $lastPid = $currentPid;
                $ifff = $ifff + 1;
                $currentPid = 0;
        }
        else
        {
                $ind = 0;
                if($ifff > 0)
                {
                        $ind = $ifff-1;
                }
                $insAry = [$cmd => $cmt];
                $jsary["pois"][$ind][$cmd] = $cmt;
        }
      }

echo json_encode($jsary);

这会将数据精美地输出为:

{
 "pois": [
 {
   "title": "Bank",
   "_poi_loc": "70.4845662217412|-84.5123291015625",
   "_poi_level": "easy",
   "_poi_type": "bank"
 },
 {
   "title": "A Creature",
   "_poi_type": "mob",
   "_poi_level": "med",
   "_poi_loc": "70.4845662217412|-84.5123291015625"
 }
]

感谢所有的答复。@Joddy - 你的帖子帮助我到达了我最终的目标。泰!

于 2013-01-04T08:15:46.703 回答