【问题标题】:explode key in json with php用php在json中爆炸键
【发布时间】:2013-09-25 08:00:07
【问题描述】:

我的 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);

【问题讨论】:

    标签: php mysql json explode


    【解决方案1】:

    您可以在 $res 上使用 array_map 来处理每个项目,然后再对其进行 json_encode。

    类似

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

    【讨论】:

    • 甜蜜!我还找到了一个有效的解决方案,正如我在回答中所编辑的那样,但你的解决方案更简单。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 2017-02-18
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    相关资源
    最近更新 更多