在json对象中删除带有0索引的额外花括号

时间:2017-01-04 13:31:17

标签: php json codeigniter object

这里我想在json对象中删除一个花括号和一个零。

我已经尝试了很多,但没有成功。

下面我发布了我的代码,请看一下。

{
  "responseCode": 200,
  "status": "success",
  "statusReason": "Home Details",
  "result": [
    {
      "branch_details": {
        "0": {  //this 0 index and one curly bracket
          "company_company_id": "12345",
          "company_name": "AVIS",
          "company_image": "uploads/avis.png",
          "company_gps": "12.3499,39.41454",
          "company_address": "dfdfhf",
          "company_phone": "8951177685",
          "company_rating": "0",
          "branch_id": "1234",
          "branch_name": "avis1",
          "branch_image": "uploads/avis.png",
          "branch_gps": "12.3499,39.41454",
          "branch_address": "eredfdf",
          "branch_phone": "6745745456465",
          "branch_rating": "0"
        },//this curly bracket
        "car_details": [
          {
            "car_id": "123",
            "name": "audi",

          },
          {
            "car_id": "14782",
            "name": "bmw",

          },
          {
            "car_id": "14781",
            "name": "nano",

          },
          {
            "car_id": "14783",
            "name": "bmw",

          }
        ]
      }
    },
    {
      "branch_details": {
        "0": {//this 0 index and one curly bracket
          "company_company_id": "12345",
          "company_name": "AVIS",
          "company_image": "uploads/avis.png",
          "company_gps": "12.3499,39.41454",
          "company_address": "dfdfhf",
          "company_phone": "8951177685",
          "company_rating": "0",
          "branch_id": "1478",
          "branch_name": "hertz1",
          "branch_image": "uploads/company2.png",
          "branch_gps": "12.3499,39.41454",
          "branch_address": "sfgsdfg",
          "branch_phone": "sdfgsdfgsdgf",
          "branch_rating": "0"
        },//this curly bracket
        "car_details": []
      }
    }
  ]
}

这里我想删除一对花括号和一个零索引,但我无法实现。

代码

这个代码在每个内部,我发布了它的一部分。

$this->db->select("IFNULL(company.company_id,'') as company_company_id,IFNULL(company.name,'') as company_name,IFNULL(company.image,'') as company_image,IFNULL(company.gps,'') as company_gps,IFNULL(company.address,'')as company_address,IFNULL(company.phone,'')as company_phone,(select IFNULL(avg(rating),0) from company_rating where user_id='$user_id'  and delete_status ='false' )as company_rating,IFNULL(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");

$this->db->from("branch");
$this->db->join("company","branch.company_id=company.company_id");
$this->db->where("branch.branch_id",$branch_id);
$this->db->where("branch.delete_status" , "false");
$this->db->where("company.delete_status" , "false");
$tempquery111 = $this->db->get();

$temp1['branch_details'] = $tempquery111->result();


$this->db->select("IFNULL(car_id,'') as car_id,IFNULL(name,'') as name,IFNULL(image,'') as 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 rating");

$car_details = $this->db->get_where("cars",array("cars.branch_id" =>$branch_id ,"delete_status" => "false"));
$temp1['branch_details']['car_details'] = $car_details->result(); 

array_push($tempquery11,$temp1);

任何人都可以提出建议吗?

1 个答案:

答案 0 :(得分:0)

在codeigniter中查询数据库时,result()将返回一个数组(即使只有一个结果),row()将返回一行。

尝试将$car_details->result();更改为$car_details->row();

道歉,我误读了json对象。我应该说要改变

$tempquery111->result();$tempquery111->row();

编辑:

尝试像这样设置你的数组。

如果它适用于company_company_id,它将适用于整个部分,您只需为每个部分执行相同操作。

$temp1['branch_details']['company_company_id'] = $tempquery111->result()->company_company_id;

我确信有更好的方法可以做到这一点,但对于我的生活,我现在想不到它。