【问题标题】:PHP Unset is resetting my Session ArrayPHP Unset 正在重置我的会话数组
【发布时间】:2015-10-16 08:44:23
【问题描述】:

我遇到了一个问题,我正在发送从会话数组中删除特定产品的请求。当结果返回时,会话为空,并且在重置时它显然具有多个值。这是代码。

    if (in_array($input_product_to_delete, $_SESSION['cart_items'])) {
        var_dump($_SESSION['cart_items']);
        var_dump($input_product_to_delete);
        unset($_SESSION['cart_items'],$input_product_to_delete);
        var_dump($_SESSION['cart_items']);
    }

当我需要它来清除一个对象时,正在输入条件并清除整个会话。 这是转储的输出。

array(3) {
    [0]=> array(12) {
        [0]=> string(1) "4"
        ["ID"]=> string(1) "4"
        [1]=> string(11) "Razor Mouse"
        ["name"]=> string(11) "Razor Mouse"
        [2]=> string(46) "Lights Up with customizable keyboard shortcuts"
        ["description"]=> string(46) "Lights Up with customizable keyboard shortcuts"
        [3]=> string(2) "65"
        ["quantity"]=> string(2) "65"
        [4]=> string(5) "29.99"
        ["price"]=> string(5) "29.99"
        [5]=> string(8) "Hardware"
        ["type"]=> string(8) "Hardware"
    }
    [1]=> array(12) {
        [0]=> string(1) "5"
        ["ID"]=> string(1) "5"
        [1]=> string(13) "Nvidia GTX970"
        ["name"]=> string(13) "Nvidia GTX970"
        [2]=> string(70) "Extremely powerful graphics card for games that are graphics intensive"
        ["description"]=> string(70) "Extremely powerful graphics card for games that are graphics intensive"
        [3]=> string(2) "20"
        ["quantity"]=> string(2) "20"
        [4]=> string(6) "525.99"
        ["price"]=> string(6) "525.99"
        [5]=> string(8) "Hardware"
        ["type"]=> string(8) "Hardware"
    }
    [2]=> array(12) {
        [0]=> string(1) "9"
        ["ID"]=> string(1) "9"
        [1]=> string(4) "tewt"
        ["name"]=> string(4) "tewt"
        [2]=> string(4) "test"
        ["description"]=> string(4) "test"
        [3]=> string(3) "123"
        ["quantity"]=> string(3) "123"
        [4]=> string(1) "2"
        ["price"]=> string(1) "2"
        [5]=> string(8) "Hardware"
        ["type"]=> string(8) "Hardware"
    }
}

array(12) {
        [0]=> string(1) "9"
        ["ID"]=> string(1) "9"
        [1]=> string(4) "tewt"
        ["name"]=> string(4) "tewt"
        [2]=> string(4) "test"
        ["description"]=> string(4) "test"
        [3]=> string(3) "123"
        ["quantity"]=> string(3) "123"
        [4]=> string(1) "2"
        ["price"]=> string(1) "2"
        [5]=> string(8) "Hardware"
        ["type"]=> string(8) "Hardware"
    }

NULL 

取消设置后会话为空。

【问题讨论】:

  • unset($_SESSION['cart_items'],$input_product_to_delete); 将取消设置 $_SESSION['cart_items'] && $input_product_to_delete - php.net/manual/en/function.unset.php。如果您只想在$_SESSION['cart_items'] 中取消设置$input_product_to_delete,则需要指定它unset($_SESSION['cart_items'][$input_product_to_delete]);
  • 哦,哇,我可能添加了一个逗号,但没有意识到它做了什么。我刚来php谢谢你的解释。我以前有使用 unset 的例子,只是不认识那个逗号。我真的很感激。

标签: php mysql session unset


【解决方案1】:

我认为你有一个非常简单的问题。 Unset 需要很多参数。但是用逗号分隔,您将删除所有这些。在您的情况下,您删除了这两个变量。

$_SESSION['cart_items']$input_product_to_delete

因此,如果您想删除数组中的一个元素,您必须将它们设置为数组数组,例如:

unset($_SESSION['cart_items'][$input_product_to_delete])

我希望能解决您的问题。

【讨论】:

  • 不太清楚为什么我在未设置时收到非法偏移类型。但我会解决那个问题。
  • 之前用isset检查你的变量。
  • 原因是因为 $input_product_to_delete 是一个对象,你不能用一个对象访问数组的索引。我修复了 foreach ($_SESSION['cart_items'] as $key => $value) { if ($value['name'] == $input_product_to_delete['name']) { unset($_SESSION['cart_items '][$key]); } }
猜你喜欢
  • 1970-01-01
  • 2019-05-11
  • 1970-01-01
  • 2017-04-10
  • 2015-08-18
  • 2018-09-05
  • 2016-04-08
  • 2023-03-12
  • 2013-05-09
相关资源
最近更新 更多