2013-09-25 39 views
0

我有这样的SQL语句在我list_user_ads()函数,将找到一个特定的用户爆炸键JSON用PHP

$row = $this->db->dbh->prepare('SELECT ad.*, (SELECT GROUP_CONCAT(img.image) FROM '.$this->config->db_prefix.'_images AS img WHERE img.aid = ad.aid) AS img FROM '.$this->config->db_prefix.'_adverts ad WHERE fid = :fid ORDER BY cr_date DESC'); 

的广告和图像,这

$res = $adverts->list_user_ads($id->fid); 
json_encode($res); 

会给我一个看起来像这样的json:

[ 
    { 
     "aid": "80", 
     "fid": "703640495", 
     "title": "gxj", 
     "text": "Hbccgg", 
     "price": "800.00", 
     "category": "10", 
     "friends_allow": "1", 
     "cr_date": "1380010359", 
     "expiry_date": "1385197959", 
     "approved": "1", 
     "ip": "80.164.52.106", 
     "name": "Morten Peter Hagh Jensen", 
     "email": "[email protected]", 
     "publish_email": "1", 
     "zip_for_pickup": "9000", 
     "views": "4", 
     "num_reports": "0", 
     "img": "703640495-1380010326490.jpg,703640495-rt804-villa-a_9.jpg" 
    }, 
    { 
     "aid": "76", 
     "fid": "703640495", 
     "title": "Hfjg", 
     "text": "Chef", 
     "price": "4645.00", 
     "category": "1", 
     "friends_allow": "1", 
     "cr_date": "1380009351", 
     "expiry_date": "1385196951", 
     "approved": "1", 
     "ip": "80.164.52.106", 
     "name": "Morten Peter Hagh Jensen", 
     "email": "[email protected]", 
     "publish_email": "1", 
     "zip_for_pickup": "9000", 
     "views": "2", 
     "num_reports": "0", 
     "img": "703640495-image_20.jpg" 
    } 
] 

图像是逗号分隔的,但我必须爆炸该密钥,所以我会得到看起来像这样的结果:

[ 
    { 
     "aid": "80", 
     "fid": "703640495", 
     "title": "gxj", 
     "text": "Hbccgg", 
     "price": "800.00", 
     "category": "10", 
     "friends_allow": "1", 
     "cr_date": "1380010359", 
     "expiry_date": "1385197959", 
     "approved": "1", 
     "ip": "80.164.52.106", 
     "name": "Morten Peter Hagh Jensen", 
     "email": "[email protected]", 
     "publish_email": "1", 
     "zip_for_pickup": "9000", 
     "views": "4", 
     "num_reports": "0", 
     "img": [{ 
      "703640495-1380010326490.jpg", 
      "703640495-rt804-villa-a_9.jpg" 
     }] 
    }, 
    { 
     "aid": "76", 
     "fid": "703640495", 
     "title": "Hfjg", 
     "text": "Chef", 
     "price": "4645.00", 
     "category": "1", 
     "friends_allow": "1", 
     "cr_date": "1380009351", 
     "expiry_date": "1385196951", 
     "approved": "1", 
     "ip": "80.164.52.106", 
     "name": "Morten Peter Hagh Jensen", 
     "email": "[email protected]", 
     "publish_email": "1", 
     "zip_for_pickup": "9000", 
     "views": "2", 
     "num_reports": "0", 
     "img": [{"703640495-image_20.jpg"}] 
    }] 

但我似乎无法弄清楚如何做到这一点。

我已经尝试使用foreach并将$ value [“img”]展开并将其放入数组,然后将该数组与$ res数组结合,但将图像分别放在json对象的底部。

可能的解决方案:

foreach($res as $key => $value) { 
    $images[] = array("images" => explode(",", $value["img"])); 
} 

$new = array_replace_recursive($res, $images); 

回答

1

您可能json_encode之前使用array_map您$资源来处理每一个项目。

喜欢的东西

$return = array_map(
    function($item) { $item['img'] = explode(',', $item['img']; return $item; }, 
    $res 
); 
json_encode($res); 
+0

甜!我还找到了一个解决方案,正如我的答案中所编辑的那样,但是你的解决方案更简单。谢谢! –