我无法在JSON响应中附加一个值

时间:2017-08-23 10:54:31

标签: php json php-5.6

这里我有一个JSON响应,在那个响应我得到的值像companyId,divisionName等...,现在我想找到companyId一个意思是我想找到什么是公司名称,所以写了一个函数,还我得到了公司名称,我的问题就是这个名字,我无法追加同一个对象

protected function getStates(){ 
        // Cross validation if the request method is GET else it will return "Not Acceptable" status
        if($this->get_request_method() != "GET"){
            $response_array['status']='fail';
            $response_array['message']='GET method only accept';
            $response_array['data']='';
            $this->response($this->json($response_array), 405);
        }else{
            $info_array = array(
                        "fields"=>"stateId,stateName,companyId",
                        "where"=>"stateStaus !='Inactive'"
                    );
            $state_data = $this->GetRecords("state",$info_array);
            if(count($state_data)>0) {
                $companyId = $state_data['companyId'];
                $check_info = array(
                        'fields'=>'companyName',
                        'where'=>'companyId = "'.$companyId.'" '
                    );
                $companyName = $this->GetNameById('company',$check_info);
                //$state_data['companyName'] = $companyName; //Company Name display
                $response_array['status']='success';
                $response_array['message']='Total '.count($state_data).' record(s) found.';
                $response_array['total_record']= count($state_data);
                $response_array['data']=$state_data;
                $this->response($this->json($response_array), 200);
            } else {
                $response_array['status']='fail';
                $response_array['message']='Record not found.';
                $response_array['data']='';
                $this->response($this->json($response_array), 506);
            }
        }
    }
  

$ this-> response($ this-> json($ response_array),200); (我得到回应)

{
"status": "success",
"message": "Total 3 record(s) found.",
"total_record": 3,
"data": [
    {
        "stateId": "3",
        "stateName": "Odisha",
        "companyId": "1"
    },
    {
        "stateId": "2",
        "stateName": "Tamil Nadu",
        "companyId": "1"
    },
    {
        "stateId": "1",
        "stateName": "Karnadaka",
        "companyId": "1"
    }
]
}

为了找到companyName我写了一个函数 $ companyName = $ this-> GetNameById('company',$ check_info); 这里我得到comapany名称(Cibla)。

  

我的预期结果

{
"status": "success",
"message": "Total 3 record(s) found.",
"total_record": 3,
"data": [
    {
        "stateId": "3",
        "stateName": "Odisha",
        "companyId": "1",
        "companyName" : "Cibla"
    },
    {
        "stateId": "2",
        "stateName": "Tamil Nadu",
        "companyId": "1",
        "companyName" : "Cibla"
    },
    {
        "stateId": "1",
        "stateName": "Karnadaka",
        "companyId": "1",
        "companyName" : "Cibla"
    }
]

}

1 个答案:

答案 0 :(得分:0)

简单地将您的公司名称添加到部门数据阵列

$companyName = $this->GetNameById('company',$check_info);
$response_array['status']='success';
$response_array['message']='Total '.count($division_data).' record(s) found.';
$response_array['total_record']= count($division_data);
$division_data[0]['companyName'] = $companyName; //<--------------Add this line
$response_array['data']=$division_data;
$this->response($this->json($response_array), 200);

修改

由于您有多个division_data,因此需要对其进行循环以添加companyName

变化

$division_data[0]['companyName'] = $companyName;

foreach($division_data as $key=>$divisions)
{
    $division_data[$key]['companyName'] = $companyName;
}