Search code examples
phparraysmultidimensional-arraycodeigniter-2

How do I store a multidimensional in a flashdata session in codeigniter using CodeIgniter Version 2.x.x


I want to save data from a multidimensional array into a flashdata session in codeigniter 2.x

foreach($value->result as $val){}
    if($val->somethinghappenedtrue){
        $arr[] = array('data' => $thethingthathappened);
    }
}
$this->session->set_flashdata($arr);

the arrays would be

[0]
    'data' => 'thing1'
[1]
    'data' => 'thing2'
[3]
    'data' => 'thing3'
[4]
    'data' => 'thing4'

i have been trying to access is by

echo "<pre>";
print_r($this->session->flashdata('arr'));
echo "</pre>";

and

echo "<pre>";
print_r($this->session->flashdata('data'));
echo "</pre>";

so that it would print the exact same arrays that are shown above but nothing is displayed on screen


Solution

  • Try this

      foreach($value->result as $val){}
           if($val->somethinghappenedtrue){
               $arr[] = $thethingthathappened;
           }
       }
       $this->session->set_flashdata('data',$arr);
    
       echo "<pre>";
       print_r($this->session->flashdata('data'));
       echo "</pre>";
    

    It will give you result as following

    Array ( [0] => thing1 [1] => thing2 [2] => thing3 )