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 github符合条件20分钟秒到账,github空投 提供github账号可兑换💰感兴趣的可以找我交流一下
    • ¥50 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥200 uniapp长期运行卡死问题解决
    • ¥15 latex怎么处理论文引理引用参考文献
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?