two dimension session issue with codeigniter

2019-06-03 13:29发布

问题:

I want to store two dimensional array in session, where i keep creating the array through a function call.

I am trying with this code

function nextQuestion($questionId,$response)
{ 
       $this->session->set_userdata("res[$questionId][]"),$response);
}

but this is not creating a two dimensional array

回答1:

The key stored in the session must be a string but the value can be a multi dimensional array. You can retrieve the array, manipulate it and save it in the session again like this:

$session_response = $this->session->userdata("res");
//some manipulation on $session_response
$this->session->set_userdata('res', $session_response);


回答2:

sametimes codeigniter session library can be boring. if i add array to session, i serialize the array.

You can serialize array and get back with unserialize. or you can use php session library.