如何删除php会话数组中的行?

如何删除php会话数组中的行?,php,arrays,session,cart,shopping,Php,Arrays,Session,Cart,Shopping,将具有“添加到购物车”功能的购物车组合在一起。除了从购物车中移除物品外,其他一切都正常。以下是我的简化版本,以便更容易地了解问题所在: $action = $_GET['action']; $id = $_GET['id']; if ($action == "remove"){ unset($_SESSION['cart'][$product_id][$id]); //Also tried this without sucess: $_SESSION['cart'][$p

将具有“添加到购物车”功能的购物车组合在一起。除了从购物车中移除物品外,其他一切都正常。以下是我的简化版本,以便更容易地了解问题所在:

$action = $_GET['action'];
$id     = $_GET['id'];
if ($action == "remove"){
    unset($_SESSION['cart'][$product_id][$id]);
    //Also tried this without sucess: $_SESSION['cart'][$product_id]--;
}

foreach($_SESSION['cart'] as $product_id) {
    echo 'Details'.$product_id['title'].' - '.$product_id['price'];
    echo '<a href="Cart.php?action=remove&id='.$product_id['product_id'].'">Remove</a>';
}
$action=$\u GET['action'];
$id=$_GET['id'];
如果($action==“remove”){
取消设置($_会话['cart'][$product_id][$id]);
//还尝试了此操作,但未成功:$\u会话['cart'][$product\u id]--;
}
foreach($\会话['cart']作为$product\ id){
回显“详细信息”。$product_id['title']。-。$product_id['price'];
回声';
}

应该能够调用:

unset($_SESSION['cart'][$id]);
这对你有用

if ($action == "remove"){
  foreach($_SESSION['cart'] as $key => $value) {
      if($value['product_id'] == $id) {
        unset($_SESSION['cart'][$key]);
       }
     }
}

现在,如果您有能力向每个产品添加数量,那么这将不会有帮助……然后您必须检查数量是否存在……并相应增加……或者类似的内容

我起初认为,但可能不会,因为我们不知道他是否将$id存储为数组键,否则他应该搜索并获得您的答案!:)让我知道它是否工作,你能帮助我,如何删除会话值?这是我的代码->和输出->@Kylie