PHP Unset正在重置我的会话数组

时间:2015-07-25 21:51:03

标签: php mysql session unset

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

    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 

取消设置后会话为空。

1 个答案:

答案 0 :(得分:0)

我认为你有一个非常简单的问题。 取消设置会占用您想要的许多参数。但是以逗号分隔,您将删除所有这些内容。在您的情况下,您删除两个变量。

$_SESSION['cart_items']$input_product_to_delete

因此,如果要删除数组中的元素,则必须将它们设置为数组数组,例如:

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

我希望能解决你的问题。