douzong2206 2017-08-23 10:54
浏览 57

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

Here i have one JSON response, in that response i am getting values like companyId,and divisionName etc..., now i want find companyId one means i want to find what is the company name, so written one function and also i got the company name, my problems is in this name i am not able to append same object

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); (i am getting response)

{
"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"
    }
]
}

For finding companyName i wrote one function $companyName = $this->GetNameById('company',$check_info); here i am getting comapany name (Cibla).

My expected results

{
"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条回答 默认 最新

  • douju6542 2017-08-23 11:00
    关注

    Simple add your company name to division data array

    $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);
    

    EDIT

    As you have multiple division_data you need to loop over them to add companyName

    change

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

    to

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

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计