Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access array variable in session (CodeIgniter)

Tags:

codeigniter

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.

like image 911
sehummel Avatar asked Jan 21 '11 15:01

sehummel


2 Answers

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.

like image 189
arnial Avatar answered Oct 30 '22 07:10

arnial


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.

like image 21
Aurelien Avatar answered Oct 30 '22 09:10

Aurelien