Unset Session Variable

I am having difficulty working with session. Here is some code:




$item_id = $_POST['item_id'];


$session = Yii::app()->session;


$basket = $session['basket'];


if(isset($basket[$item_id]))

{

	unset($basket[$item_id]);

}



The ‘unset’ does not work - instead I have to do:


unset($_SESSION['basket'][$item_id]);

Anyone know why it only works this way?

Because in your case $basket holds a copy of $session[‘basket’], so unsetting $basket has no effect to $session.

You may want to pass that by reference, like this: $basket = &$session[‘basket’];