I have two questions. Both involve CodeIgniter’s session support when using the database as a backend.
1. I’m wondering if there is a way of unsetting an inner element of an array against a session when using CodeIgniter’s Database Session support.
For example, in standard PHP this sort of functionality is possible:
unset($_SESSION['items'][$item_name]);
However, CodeIgniter seems to work like this:
$this->session->unset_userdata('items'); // What about unsetting $item_name specifically?
2. And I’m not sure the best way of unsetting an individual array item.
The other thing I would like to be able to do is to check whether an inner element of an array is set.
For example, in standard PHP this sort of functionality is possible:
if (isset($_SESSION['items'][$item_name])) {
}
But in CodeIgniter we have something like this:
if ($this->session->userdata('items') !== FALSE) {
}
The equivalent to
$_SESSIONin Codeigniter isThis might look like the same named function name
to you, but in fact it’s an array, like
$_SESSION. With this information, let’s look at your specific questions:If you would like to access that more easily, you can create a reference/alias:
To apply the changes to the session variable(s), you need to call
to store the values into the database. That’s better than using the
userdata()function, because it will trigger a write in the database whenever you set a single value.