gpt4 book ai didi

javascript - 如何在 CI 中向 json 对象添加循环数据?

转载 作者:行者123 更新时间:2023-12-02 13:44:30 25 4
gpt4 key购买 nike

我试图将循环数据合并到 json 对象,每次我只得到循环的最后一个结果,所以我将该变量声明为数组,它来得很好,我得到了结果,但格式不正确,有额外的方括号,这是我不想要的,因为在前端解析时,它会产生问题。

下面我提供了我的代码。

{

"status": "success",
"statusReason": "Favouritelist",
"result": {
"Favouritelist": [
[ //want to remove this square bracket
{
"branch_id": "1234",
"branch_name": "avis1",
"branch_image": "uploads/avis.png",
"branch_gps": "12.9250,77.5938",
"branch_address": "eredfdf",
"branch_phone": "2147483647",

}
],//want to remove this square bracket
[//want to remove this square bracket
{
"branch_id": "1234",
"branch_name": "avis1",
"branch_image": "uploads/avis.png",
"branch_gps": "12.9250,77.5938",
"branch_address": "eredfdf",
"branch_phone": "2147483647",


}
]//want to remove this square bracket
]
}
}

我的 CI 代码

foreach ($tempquery->result() as $res1){
$car_id = $res1->car_id;

if(empty($timestamp)){

$this->db->select("IFNULL(branch.branch_id,'') as branch_id,IFNULL(branch.name,'') as branch_name,IFNULL(branch.image,'') as branch_image,IFNULL(branch.gps,'') as branch_gps,IFNULL(branch.address,'')as branch_address,IFNULL(branch.phone,'')as branch_phone,(select IFNULL(avg(rating),0) from branch_rating where user_id='$user_id' and delete_status ='false' )as branch_rating,IFNULL(car_id,'') as car_id,IFNULL(cars.name,'') as car_name,IFNULL(cars.image,'') as car_image,IFNULL(model,'') as model,IFNULL(price,'')as price,IFNULL(year,'')as year,IFNULL(type,'')as type,IFNULL(`no_of_seats`,'')as `no_of_seats`,IFNULL(`gear_type`,'') as `gear_type`,IFNULL(color,'') as color,insurance,status,IFNULL(description,'') as description,(select IFNULL(avg(rating),0) from car_rating where user_id='$user_id' and delete_status ='false' )as car_rating,car_types.typename AS carType,cars.petrol_type,car_types.car_type_id,cars.delivery_charges,cars.services");
$this->db->from("branch");
$this->db->join("cars","branch.branch_id=cars.branch_id");
$this->db->join('car_types', 'cars.car_type_id = car_types.car_type_id');
$this->db->where("cars.car_id",$car_id);
$this->db->where("branch.delete_status" , "false");
$this->db->where("cars.delete_status" , "false");
$car_details= $this->db->get();
$cdetail_new[]=$car_details->result(); ///added [] to mearge dat


}

Response::code_200("Favouritelist", array("Favouritelist" => $cdetail_new));

任何人都可以建议我,我哪里做错了..

ADV 谢谢

最佳答案

看起来源代码中包含了数组。这里的 result() 函数也许将其作为数组提供?

$car_details->result();

如果是这种情况,您可能想在此处打开它。如果您使用的是最新版本的 PHP,则可以使用此表示法:

$cdetail_new[] = $car_details->result()[0];

如果没有,请尝试旧的:

$result = $car_details->result();
$cdetail_new[] = $result[0];

您可以检查这是否是问题所在,即,result() 是否以包含单个元素的数组形式提供输出:

$car_details = $this->db->get();
var_dump($car_details->result()); // Is this resulting as an array?
die();
$cdetail_new[] = $car_details->result(); ///added [] to mearge dat

关于javascript - 如何在 CI 中向 json 对象添加循环数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41519128/

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