0

Can someone quickly help me out with CodeIgniter's syntax. I need to access an array I stored in the session's userdata and I cant figure out the proper syntax.

<?php echo $this->session->userdata['user_session']['first_name']; ?>

gives me this error:

Fatal error: Cannot use object of type stdClass as array

All of the answers given in this Question dont work: Access array variable in session (CodeIgniter)

2
  • var_dump($this->session->userdata); and read the error message carefully (you will wonder but it's output so that you could read it) Commented Jul 21, 2013 at 3:14
  • please show us the output of var_dump($this->session->userdata); Commented Jul 21, 2013 at 5:25

3 Answers 3

0

This is how you get session data:

echo $this->session->userdata('first_name');
Sign up to request clarification or add additional context in comments.

Comments

0

Been a while since I've worked in Codeigniter, but if I can remember correctly, when you store an array like you've stated, you'd call it like this:

$this->session->userdata("user_session")['first_name'];

Let me know if that works?

Or you can store that data to a variable, and call the array that way. Like such:

$data = array("bar" => "the_value");
$this->session->set_userdata("foo", $data);
$foo = $this->session->userdata("foo");
echo $foo["bar"]; //Outputs the_value

Let me know if that helped.

However, just to let you know.. Normally, storing the session data goes as follows:

$this->session->set_userdata("first_name", "value");

Really no need to go and set your own array inside of userdata, because that's generally what the userdata array is for.

Comments

0

I found the proper syntax. Well, at least one way to go about it. @Matt GrubB was the closest and put me on the right track.

$temp_session = $this->session->userdata('user_session');
echo $temp_session->first_name;

Since userdata is an object full of info created when I query my database, the easiest way to access the data is to take it and put it in another temporary array. You then have to stab it. I kept stumbling by trying to do $this->temp_session->first_name or something of the like.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.