如何将循环数据添加到CI中的json对象?

时间:2017-01-07 07:44:18

标签: javascript php json codeigniter

我正在尝试将循环数据合并到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

1 个答案:

答案 0 :(得分:2)

看起来源包含数组。这里的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