0

我的 list_user_ads() 函数中有这个 SQL 语句,它将为特定用户找到广告和图像

$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": "xxx@xxx.dk",
        "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": "xxx@xxx.dk",
        "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": "xxx@xxx.dk",
        "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": "xxx@xxx.dk",
        "publish_email": "1",
        "zip_for_pickup": "9000",
        "views": "2",
        "num_reports": "0",
        "img": [{"703640495-image_20.jpg"}]
    }]

但我似乎无法弄清楚该怎么做。

我尝试过使用 foreach 和 explode $value["img"] 并将其放入一个数组中,然后将该数组与 $res 数组连接起来,但这会将图像分别放在 json 对象的底部。

可能的解决方案:

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

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

1 回答 1

1

您可以在 $res 上使用 array_map 在 json_encode 之前处理每个项目。

就像是

$return = array_map(
    function($item) { $item['img'] = explode(',', $item['img']; return $item; },
    $res
);
json_encode($res);
于 2013-09-25T08:41:42.823 回答