douyin7829 2019-01-22 13:12
浏览 36

codeigniter:获取响应为json对象

I have a REST_Controller in which following code sends the json response

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
}

Working perfectly however for a particular index I am getting it in json object but I want it in array

In my controller I have

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

In my Model I have

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();
    }

}

the response I get for $data['form'] is normally this;

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

Which is an array but when I have gender = Male and caption = pregnancy I get

"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
    }
},

which is json object. What am I doing wrong?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 BV260Y用MQTT向阿里云发布主题消息一直错误
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序