我在购物车会话(数组)中添加了一些商品,但我想删除1行,因为我编写了以下代码,但这对我不起作用。
public function deletecart() {
$this->loadModel("Product");
if($this->Session->check('cart') AND count($this->Session->read('cart'))>0)
{
foreach ($this->Session->read('cart') as $key => $value) {
if ($value['0']['Product']['id'] == "12") {
unset($this->Session->read('cart')[$key]);
}
}
}
}
这是我的会话调试值
array(
'[0]' => array(
(int) 0 => array(
'Product' => array(
'id' => '8',
'category' => 'Pendant',
)
)
),
(int) 1 => array(
(int) 0 => array(
'Product' => array(
'id' => '12',
'category' => 'Pendant'
)
)
)
)
答案 0 :(得分:1)
您无法像这样取消设置会话密钥值。您必须将Session键值存储在临时变量中。
$sessionArr = $this->Session->read('cart');
foreach ($sessionArr as $key => $value) {
if ($value['0']['Product']['id'] == "12") {
// Unset key
unset($sessionArr[$key]);
}
}
// Assign $sessionArr value to cart key
$this->Session->write('cart',$sessionArr);