Foreach数据CodIgniter时非法字符串偏移量

时间:2015-11-30 07:48:53

标签: php codeigniter

我在Controller中有这样的代码:

public function akses(){
    $username = $this->input->post('username');
    $password = $this->input->post('password');

    $this->form_validation->set_rules('username','username','required|trim|max_length[36]');
    $this->form_validation->set_rules('password','password','required|trim|max_length[500]');


    if($this->form_validation->run()==true){
        $data = array("UserName" => $username, "Password" => $password);
        $data_string = json_encode($data);
        $ch = curl_init($this->ws_url->GetUrl('UserLogin'));
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string))
        );
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        //execute post
        $result = json_decode(curl_exec($ch), true);
        //close connection
        curl_close($ch);

        if(count($result['User']) != NULL){
            echo "Data Found";

            foreach($result['User'] as $detail){
                echo $detail['IdUser'];
        }


        }
    }
}

我想获取$detail['IdUser']的数据。但是当我试图得到它时,它返回错误Illegal string offset'IdUser'。但是数据被发现了。我直接检查了我的网络服务它没有问题。

当我尝试var_dump($result['User'])时,它也没有问题。为什么当我预先处理数据时,它会像我上面说的那样返回错误?对我有什么解决方案?感谢

修改 这是var_dump($result['User'])的结果: array(6) { ["IdUser"]=> string(36) "fad706d1-b481-f1cb-81fa-9009ee7d75d4" ["Username"]=> string(4) "kugi" ["Password"]=> string(4) "kugi" ["Email"]=> string(14) "kugi@big.go.id" ["Fullname"]=> string(33) "Katalog Unsur Geografis Indonesia" ["IdentityNumber"]=> string(10) "1001001001" }

1 个答案:

答案 0 :(得分:1)

您不需要使用foreach循环。你已经掌握了这些信息:

public function akses(){
$username = $this->input->post('username');
$password = $this->input->post('password');

$this->form_validation->set_rules('username','username','required|trim|max_length[36]');
$this->form_validation->set_rules('password','password','required|trim|max_length[500]');


if($this->form_validation->run()==true){
    $data = array("UserName" => $username, "Password" => $password);
    $data_string = json_encode($data);
    $ch = curl_init($this->ws_url->GetUrl('UserLogin'));
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string))
    );
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    //execute post
    $result = json_decode(curl_exec($ch), true);
    //close connection
    curl_close($ch);

    if(count($result['User']) != NULL){
        echo "Data Found";
        echo $result['User']["IdUser"];
    }
}
}