I thought this would be simple but I cant seem to get it to work. All I want to do is to add a value into a userdata array. If a value is already in the viewed_news_items array I do not want to replace it.
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true,
'viewed_news_items' => array()
);
$this->session->set_userdata($data);
insert value into viewed_news_items array
$a = array($desk_id => $desk_id);
$this->session->set_userdata('viewed_news_items', $a);
You’re using
$desk_idas both the key and value, meaning unless you already have the value of$desk_id, you won’t be able to look it up in the array.Instead of this:
You probably wanted to do this:
That is, use the string
'desk_id'as the key whose corresponding value is$desk_id.You can push items onto the end of an array via
array_push($array, $value)or$array[] = $value. PHP will automatically assign the next numeric ID as the index for the new array element.In your current scenario, you’ll have to pull the existing list of IDs out of the session, append an ID to it, and then put it back into the session: