简体   繁体   中英

Unset the values behind a key and that key in an associative array

I'd like to say I already took at this and this answer.

Problem: I have a session variable called "cart" which looks like this.

array (
  'cart' => 
  array (
    2 => 
    array (
      0 => '2',
      1 => 2,
    ),
  ),
)

Inside of cart I want to remove the values behind the key 2 so [0 => 2, 1 => 1] . I also want 2 to begone. So it's just a blank array. cart => [] . To acomplish this I have this line.

unset($_SESSION["cart"][$id]);

$id is from a get request. I'll show the code for it if that is needed for clarification. However, the result of the above snippet gives me this instead.

array (
  3 => 
  array (
    0 => NULL,
    1 => 1,
  ),
)

Not only does 2 increment to 3 , NULL also appears for some reason.

$_SESSION["cart"] is made here in cart.inc.php

<?php
session_start();

// A cart is structured like this
// cart = [item_id: [item_id, amount], item_id: [item_id, amount]]
// item_id is the key to another array that contains the item_id, again and the amount of that item.

if (isset($_GET["remove"])) {
  $id = $_GET["remove"];
  // Instead of removing the key "id" and all of the values behind it.
  // It only changes item_id into NULL.
  unset($_SESSION["cart"][$id]);
}

$id = $_GET["id"];

if (isset($_SESSION["cart"])) {
  // If the item already exists in the cart modify the existing
  // item values instead of creating a new one.
  if (isset($_SESSION["cart"][$id])) {
    // If the user has changed the amount then this will run.
    // Instead of adding by 1 it changes the amount of whatever 
    // the new amount is.
    if (isset($_GET["amt"])) {
      $amt = intval($_GET["amt"]);
      $_SESSION["cart"][$id][1] = $amt;
    } else {
      // to increase the item quantity. So + 1 here.
      $_SESSION["cart"][$id][1] = $_SESSION["cart"][$id][1] + 1;
    }
  } else {
    // This is if the cart exists, but that item is one that isn't in the cart.
    array_push($_SESSION["cart"], [$id, 1]);
  }
} else {
  // When the cart doesn't exist, set the new cart session variable.
  $_SESSION["cart"] = [$id => [$id, 1]];
}

header("location: ../../index.php"); 

there are multiple problems, so it is easier to past fixed version here neither in comments:

<?php
session_start();

if (!isset($_SESSION["cart"])) $_SESSION["cart"] = []; // initialize card if needed

if (isset($_GET["remove"])) {
  $id = intval($_GET["remove"]);
  if (isset($_SESSION["cart"][$id])) unset($_SESSION["cart"][$id]);
}
else { // you want this part to be executed only when you add, not when you remove
    $id = isset($_GET["id"]) ? intval($_GET["id"]) : 0; // check that id is actually passed and covert it immediately
    if ($id > 0) {
        $prev_amt = isset($_SESSION["cart"][$id]) ? $_SESSION["cart"][$id][1] : 0; // initialize previous amount
        $amt = isset($_GET["amt"]) ? intval($_GET["amt"]) : ($prev_amt + 1); // intialize amount

        if ($amt > 0) $_SESSION["cart"][$id] = [$id, $amt];
    }
}

header("location: ../../index.php"); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM