Access array variable in session (CodeIgniter)

后端 未结 3 1402
攒了一身酷
攒了一身酷 2021-02-08 08:14

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[\         


        
相关标签:
3条回答
  • 2021-02-08 08:51

    Always escape your string it should be this way:

    echo $this->session->userdata('config[\'item\']'); 
    
    0 讨论(0)
  • 2021-02-08 08:52

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题