arnold_p
arnold_p

Reputation: 505

Illegal String Offset While Foreach Data CodIgniter

I have the code in Controller like this:

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'];
        }


        }
    }
}

I want to get the data of $detail['IdUser']. But when I try to get it, it returns error Illegal string offset 'IdUser'. But the data was found. I checked it directly to my web service it has no problem.

And when I try to var_dump($result['User']) it has no problem too. Why when I foreach the data, it is returns error like I said above? Any solution for me? Thanks

EDIT This is the result of 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) "[email protected]" ["Fullname"]=> string(33) "Katalog Unsur Geografis Indonesia" ["IdentityNumber"]=> string(10) "1001001001" }

Upvotes: 0

Views: 145

Answers (1)

Ilanus
Ilanus

Reputation: 6928

You don't need to use a foreach loop. you already have this information:

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

Upvotes: 1

Related Questions