gpt4 book ai didi

php - 用 php explode json 中的 key

转载 作者:行者123 更新时间:2023-11-29 13:34:05 25 4
gpt4 key购买 nike

我的 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 并分解 $value["img"] 并将其放入一个数组中,然后将该数组与 $res 数组连接起来,但这将图像单独放在 json 对象的底部。

可能的解决方案:

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

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

最佳答案

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

类似的东西

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

关于php - 用 php explode json 中的 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18999363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com