结合Json Encode的行

时间:2013-01-03 11:29:17

标签: php mysql merge json

在输出json之前,我无法合并数据。目标是让json中的每个对象包含每个“post”的所有数据。

以下代码:

$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"
 }

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

提前致谢!

2 个答案:

答案 0 :(得分: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);

尝试并发布您最终的内容。

答案 1 :(得分: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 - 你的帖子帮助我到达了我最终的位置。 TY!