如何从多维cakephp会话中删除元素

时间:2018-05-10 07:30:24

标签: session cakephp-3.0

我正在电子商务网站上工作,所有购物车产品都存放在完美运行的会话中。 这是购物车会议的调试。

debug($this->request->getsession()->read('cart'));

[
    (int) 1 => [
        (int) 0 => [
            'id' => (int) 1,
            'picture' => '1_1.webp',
            'sku' => 'TH447WA38OUMINDFAS',
            'name' => 'The Vanca Multicoloured Printed Strappy Top',
            'size' => 'S',
            'price' => '480'
        ]
    ],
    (int) 2 => [
        (int) 0 => [
            'id' => (int) 2,
            'picture' => '2_1.webp',
            'sku' => 'AL384WA86QOSINDFAS',
            'name' => 'All About You Pink Embroidered Blouse',
            'size' => 'S',
            'price' => '1330'
        ]
    ],
    (int) 3 => [
        (int) 0 => [
            'id' => (int) 3,
            'picture' => '3_1.webp',
            'sku' => 'RE367WA35NDKINDFAS',
            'name' => 'Renka Comfortable Black Color Seamless Summer Tops For Women',
            'size' => 'S',
            'price' => '495'
        ]
    ]
]

现在我想删除购物车中的任何行,但这对我不起作用。

unset($this->request->getsession()->read('cart')[1]);

1 个答案:

答案 0 :(得分:1)

应该只是

$this->request->getSession()->delete('cart.1');

访问会话数组时可以使用点表示法

您也可以在一个命令中读取和删除数据

$cart =  $this->request->getSession()->consume('cart');

请参阅手册here以及API herehere