title: How to get session array data CodeIgniter
body: <p>I'm not quite sure why the username didn't appear on view page if i change the session data into array (i'm new in CodeIgniter). I have <strong><code>auth</code> controller</strong> to make login process:</p>
  
  <pre><code>function index() {
              $this-&gt;load-&gt;library('form_validation');
              $this-&gt;form_validation-&gt;set_rules('username', 'username', 'required|trim|xss_clean');
              $this-&gt;form_validation-&gt;set_rules('password', 'password', 'required|trim|xss_clean');
  
          if ($this-&gt;form_validation-&gt;run() == FALSE) {
              $this-&gt;load-&gt;view('Login');
              # code...
          }
          else {
              $username = $this-&gt;input-&gt;post('username');
              $password = $this-&gt;input-&gt;post('password');
              $check = $this-&gt;M_login-&gt;check($username, $password);
  
              // session data
              if ($check-&gt;num_rows() == TRUE) {
                  foreach ($check-&gt;result() as $value) {
                      $sess_data['id']            = $value-&gt;id;
                      $sess_data['name']          = $value-&gt;name;
                      $sess_data['username']      = $value-&gt;username;
                      $sess_data['password']      = $value-&gt;password;
                      $sess_data['description']   = $value-&gt;description;
                      $this-&gt;session-&gt;set_userdata($sess_data);                        
              }
              redirect('Dashboard');
          }
          else {
              $this-&gt;session-&gt;set_flashdata('result_login', '&lt;br&gt;Invalid username or password, try again.');
              redirect('Login');
          }
  </code></pre>
  
  <p>And here is my <strong>dashboard controller</strong>:</p>
  
  <pre><code>public function __construct() {
              parent::__construct();
              if(!$this-&gt;session-&gt;userdata('id')){
                  redirect('login');
              }
      }
  
      public function index() {
          // Dashboard view
          $username = $this-&gt;session-&gt;userdata('username');
  
          $data['username']=$username;
  
          $this-&gt;load-&gt;view('Dashboard', $data);    
      }
  
      function logout() {
          $this-&gt;session-&gt;sess_destroy('id');
          redirect('login');
      }
  </code></pre>
  
  <p>With above code, i can get the username on my dashboard view by <code>echo $username</code>. But when i change the session data like this:</p>
  
  <pre><code>if ($check-&gt;num_rows() == TRUE) {
     foreach ($check-&gt;result() as $value) {
  
     // session data
     $sess_data = array(
                 'id'           =&gt; $value-&gt;id,
                 'username'     =&gt; $value-&gt;username,
                 'password'     =&gt; $value-&gt;password,
                 'name'         =&gt; $value-&gt;name,
                 'description'  =&gt; $value-&gt;description
               );
     $this-&gt;session-&gt;set_userdata('log',$sess_data);
     }
  }
  </code></pre>
  
  <p>And <strong>Dashboard controller</strong> changed like this:</p>
  
  <pre><code>if(!$this-&gt;session-&gt;userdata('id')) {
       $getdata= $this-&gt;session-&gt;userdata('log');
  
       $data['username'] = $getdata['username'];
  }
  $this-&gt;load-&gt;view('Dashboard', $data);
  </code></pre>
  
  <p>Then the username disappeared from view page. How can i store username in session array and call it in view page. Please share your better suggestions or your experience guys.</p>
  
  <p>Thanks.</p>