11

I have an array called config. I'm trying to echo a variable from the array in the session.

I've tried:

echo $this->session->userdata('config['item']'); 

but it doesn't work. What's wrong with my syntax here? I've print_r'd my session and the items are in the config array. I've also tried:

echo $this->session->userdata("config['item']");

I get no errors this time, but no data either.

3 Answers 3

19

If config is an array . And item is string name of what you want to get from config then

echo $this->session->userdata($config['item']);

or

echo $_SESSION[$config['item']];

If config is an array inside session you should first get it.

$tmp = $this->session->userdata('config');
echo $tmp['item'];

or

echo $_SESSION['config']['item'] 

Sorry for my english.

Sign up to request clarification or add additional context in comments.

1 Comment

why do i get undefined index whenever i do $abc = $this->session->userdata('datauser'); echo $abc['ID_NUM'];
9

If you want to use the session array, use the variable, not the function:

echo $this->session->userdata['user_data']['item'];

If you want to write:

$this->session->userdata['user_data']['item'] = 'value';
$this->session->userdata['other_data']['other'] = 'value2';
$this->session->sess_write();

This allows you to edit values in array just like you do with $_SESION['user_data']['avatar'] = $avatar, with 'only' one extra line and only using CI library.

Comments

0

Always escape your string it should be this way:

echo $this->session->userdata('config[\'item\']'); 

3 Comments

Yeah, I realized I had a quoting issue. But this yields no data even though I can print_r it.
then u must see this codeigniter.com/user_guide/libraries/config.html how to access ur config vars
The config array is in my userdata. Could the two be colliding?

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.