如何在Yii会话数组中取消设置项目

时间:2014-08-06 17:08:48

标签: php arrays session yii unset

我的会话数组

$session = Yii::app()->session;
$session['product']=array(0=>array('id'=>'1','name'=>'Cocacola'),1=>array('id'=>'2','name'=>'Pepsi'));

print_r($session['cart']);
//output is
//Array ( [0] => Array ( [id] => 1 [name] => Cocacola ) [1] => Array ( [id] => 1 [name] => Pepsi ) )

现在我想从我的数组中取消设置一个产品

unset($session['cart'][0]);//unset Cocacola Product

print_r($session['cart']);
//output is
//Array ( [0] => Array ( [id] => 1 [name] => Cocacola ) [1] => Array ( [id] => 1 [name] => Pepsi ) )

但两个输出都是一样的。 unset()不工作。请帮我解决一下。 提前致谢。

1 个答案:

答案 0 :(得分:1)

这是你的$ session

$session['product']=array(0=>array('id'=>'1','name'=>'Cocacola'),1=>array('id'=>'2','name'=>'Pepsi'));

尝试这样,使用$ temp array

$temp = $session['product'];
unset($temp[0]);
$session['product'] = $temp;