codeigniter:获取响应作为json对象

时间:2019-01-22 13:12:42

标签: php arrays codeigniter

我有一个REST_Controller,其中以下代码发送json响应

if ($data)
{
        // Set the response and exit
        $this->response($data, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
}
else
{
     // Set the response and exit
     $this->response([
     'status' => FALSE,
        'message' => 'response message',
     ], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
}

但是对于特定的索引来说,它工作得很好,但是我在json对象中获取它,但是我希望在数组中使用它

我的控制器中有

$data['form']=$this->Admin_model->getAnsForm($data['question']['id'], $params);

在我的模型中,我有

public function getAnsForm($qId, $params)
{

    if($params['pathway']==4)
    {
        $data=array();
        $data[0]=$this->db->select('*')->from('ans_form')->where('question',$qId)->get()->result_array();

        if(count($data[0])>0)
        {
            if($params['gender']=='male' || $params['gender']=='Male')
            {
                $d['form'][0]=array();
                for($i=0;$i<count($data[0]);$i++)
                {
                    if($data[0][$i]['caption']=='pregnancy')
                    {
                        // echo '<b>Male</b>';exit;
                    }
                    else
                    {
                        $d['form'][0][$i]=$data[0][$i];                            
                    }
                }
                $data[0]=$d['form'][0];
                return $data[0]; 
            }
            else
            {
                return $data[0];
            }

        }
        else
        {
            return array();
        }

    }
    else
    {
        return $this->db->select('*')->from('ans_form')->where('question',$qId)->get()->result_array();
    }

}

我对$ data ['form']的响应通常是这样;

"form": [
    {
        "id": "131",
        "question": "50",
        "type": "radio",
        "name": "score",
        "value": "0",
        "caption": "10 or less",
        "placeholder": null
    },
    {
        "id": "132",
        "question": "50",
        "type": "radio",
        "name": "score",
        "value": "1",
        "caption": "11-20",
        "placeholder": null
    },
    {
        "id": "133",
        "question": "50",
        "type": "radio",
        "name": "score",
        "value": "2",
        "caption": "21-30",
        "placeholder": null
    },
    {
        "id": "134",
        "question": "50",
        "type": "radio",
        "name": "score",
        "value": "3",
        "caption": "31 or more",
        "placeholder": null
    }
],

这是一个数组,但是当我的性别=男性且标题=怀孕时,我会得到

"form": {
    "0": {
        "id": "145",
        "question": "54",
        "type": "checkbox",
        "name": "score[]",
        "value": "1",
        "caption": "Mental health problems",
        "placeholder": null
    },
    "1": {
        "id": "146",
        "question": "54",
        "type": "checkbox",
        "name": "score[]",
        "value": "2",
        "caption": "Misuse substances",
        "placeholder": null
    },
    "2": {
        "id": "147",
        "question": "54",
        "type": "checkbox",
        "name": "score[]",
        "value": "3",
        "caption": "Smoking-related illness (for example lung cancer)",
        "placeholder": null
    },
    "3": {
        "id": "148",
        "question": "54",
        "type": "checkbox",
        "name": "score[]",
        "value": "4",
        "caption": "Asthma",
        "placeholder": null
    },
    "4": {
        "id": "149",
        "question": "54",
        "type": "checkbox",
        "name": "score[]",
        "value": "5",
        "caption": "Cardiovascular disease",
        "placeholder": null
    },
    "5": {
        "id": "150",
        "question": "54",
        "type": "checkbox",
        "name": "score[]",
        "value": "6",
        "caption": "Chronic obstructive pulmonary disease",
        "placeholder": null
    },
    "6": {
        "id": "151",
        "question": "54",
        "type": "checkbox",
        "name": "score[]",
        "value": "7",
        "caption": "diabetes mellitus",
        "placeholder": null
    },
    "8": {
        "id": "209",
        "question": "54",
        "type": "checkbox",
        "name": "score[]",
        "value": "0",
        "caption": "None of above",
        "placeholder": null
    }
},

是json对象。我在做什么错了?

0 个答案:

没有答案