【问题标题】:How to remove element from multidimensional cakephp session如何从多维 cakephp 会话中删除元素
【发布时间】:2018-10-20 10:10:54
【问题描述】:

我正在开发电子商务网站,该网站存储了会话中的所有购物车产品,并且运行良好。 这是购物车会话的调试。

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]);

【问题讨论】:

    标签: session cakephp-3.0


    【解决方案1】:

    应该很简单

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

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

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

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

    请参阅手册 here 和 API herehere

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多