简体   繁体   中英

PHP CodeIgniter Foreach Returning Incorrect Data

Hello awesome programmers!

I am so sorry for the the novice question, however, I am having trouble finding a solution to my problem. I am attempting to run a for each loop through my array passed from my controller, however, the data being outputted is not the same as when I run a var_dump($array). I am thinking perhaps that I need to iterate through this object maybe? However, when I attempt to do so, I get a non-object error.

Controller:

$data['user_details'] = $this->ion_auth->user()->row();

View:

var_dump($user_details);
    foreach($user_details as $item){
        echo $item['email'];
    }

The output of this is : "21nfn4111NR12" but should be roger@peterson.net!

I have also tried the object form:

var_dump($user_details);
    foreach($user_details as $item){
        echo $item->email;
    }

However, it results in error trying to get property of non-object!

When I run the var dump I get the following:

object(stdClass)#21 (17) {
  ["id"]=>
  string(1) "2"
  ["ip_address"]=>
  string(14) "119.132.127.01"
  ["username"]=>
  string(12) "roger petereson"
  ["password"]=>
  string(40) "fdZxF/RQo4nZKmbA5XQlwefbc8f8e5c74899c3d0"
  ["salt"]=>
  NULL
  ["email"]=>
  string(19) "roger@peterson.net"
  ["activation_code"]=>
  NULL
  ["forgotten_password_code"]=>
  NULL
  ["forgotten_password_time"]=>
  NULL
  ["remember_code"]=>
  string(22) "44hjOlloLTIrkSrjSBVNie"
  ["created_on"]=>
  string(10) "1404939094"
  ["last_login"]=>
  string(10) "1405099607"
  ["active"]=>
  string(1) "1"
  ["first_name"]=>
  string(6) "Roger"
  ["last_name"]=>
  string(5) "Peterson"
  ["is_owner"]=>
  string(1) "1"
  ["user_id"]=>
  string(1) "2"
}

The following line:

$this->ion_auth->user()->row(); 

returns an object not an array (check your var_dump output), so you just need to

echo $user_details->email;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM