Search code examples
phpmysqlsessionunset

PHP Unset is resetting my Session Array


I am encountering an issue where i am sending a request to delete a specific product out of a session array. When the results return the session is null and it clearly has more than one value in it when resetting. Here is the code.

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

The conditional is being entered and clears the entire session when i need it to just clear the one object. Here is the output from the dumps.

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 

The session is null after the unset.


Solution

  • I think you have a very simple problem. Unset takes many parameters you want. But with a comma separated you delete all of them. In your case you delete both variables.

    $_SESSION['cart_items'] and $input_product_to_delete

    So if you want to delete an element of your array you have to set them as array array for example:

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

    i hope that solve your problem.