2

Model:

Grabs data from mySQL with the value: array['10','5','2'] and puts it in the session data

Controller:

Puts that session data into an array to pass to the view:

$fubar = array(
'ex1' => $this->session->userdata('ex1')
    );
$this->load->view('view', $fubar);

View:

echo $ex1;

The view will spit out the array: array['10','5','2'] How do I parse that array to get the individual values (10, 5, or 2)? For example, echo $ex1['1']; will spit out the first 1 characters: ar but I want it to return the value 5

I think perhaps I may not be storing the array properly in mySQL, but I'm not sure.

5
  • Please, put var_dump($fubar); before $this->load->view('view', $fubar); What does it display? Commented Sep 8, 2012 at 21:09
  • var_dump: array(4) { ["ex1"]=> string(21) "array['40','20','10']" ["ex2"]=> string(21) "array['80','40','20']" ["ex3"]=> bool(false) ["date"]=> string(10) "2012-09-08" } Commented Sep 8, 2012 at 21:11
  • So, the arrays are not arrays at all, they're just strings. How do I parse those strings and save them as arrays? Commented Sep 8, 2012 at 21:13
  • 1
    There is eval(<php code here>). But it's not safe. It's very strange that you save array as string. it's better to use serialize($your_array) and unserialize($your_serialized_array) functions or json_encode() and json_decode() functions. In conclusion: 1. Save in session: serialize($your_array); 2. Extract from session: unserialize($this->session->userdata('ex1')) Commented Sep 8, 2012 at 21:20
  • 2
    Why are your arrays being returned as strings? Post your model, or whatever is making this assignment. Commented Sep 8, 2012 at 23:22

1 Answer 1

1
$fubar['ex1'] = $this->session->userdata('ex1');
$this->load->view('view', $fubar);

Now Try

    $ex1[0];
    $ex1[1];
Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly the same thing than $fubar = array('ex1' => $this->session->userdata('ex1') );

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.