doujiepin5547 2013-09-25 08:00
浏览 9
已采纳

使用php在json中爆炸键

I have this SQL statement in my list_user_ads() function that will find an adverts and images for a specific user

$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');

and this

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

will give me a json that looks like this:

[
    {
        "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"
    }
]

The images are commaseparated, but I have to explode that key so I will get a result that will look like this:

[
    {
        "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"}]
    }]

But I can't seem to figure out how to do it.

I have tried with a foreach and explode $value["img"] and put it into an array and then join that array with the $res array, but that put the images separately at the bottom of the json object.

POSSIBLE SOLUTION:

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

$new = array_replace_recursive($res, $images);
  • 写回答

1条回答 默认 最新

  • dragon_9000 2013-09-25 08:41
    关注

    You may use an array_map on your $res to process each item before json_encode it.

    Something like

    $return = array_map(
        function($item) { $item['img'] = explode(',', $item['img']; return $item; },
        $res
    );
    json_encode($res);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址