Php从多维数组购物车中删除特定项目

时间:2018-05-10 08:05:34

标签: php

我有一个用于购物车的数组。我正在尝试从购物车中删除特定商品。我已经尝试使用数组的键值和unset()这对我来说不起作用。

以下是我的代码示例。无法找出最佳方法

<?php if (isset($_SESSION['cart'])!='') :?>   
<div class="col-md-6 well pull-right My-cart">
    <ol class = "Shopping-list">
        <form class = "Delete-from-cart" method = "POST">
            <?php $sum = 0; ?>
            <?php foreach ($_SESSION['cart'] as $Key => $row) : ?> 
            <li><?php echo $row['Title'] .' * '.$row['quantity']; ?> <i class="fas fa-trash-alt delete-icon"></i>
                <?php $total =    $sum+= $row['Price'] * $row['quantity']; ?>
                <input type="hidden" name="Deleted-ID" value="<?php echo $Key; ?>">
            </li>
            <?php endforeach; ?>
        </form>   
    </ol>
    <div class="col-sm-6 well pull-right Total">
        <?php echo 'Your total is '.$total; ?>
    </div>
</div>
<?php endif;?>

session_start();
$id = filter_var($_POST['Phone-ID'], FILTER_SANITIZE_NUMBER_INT);
$Phone_Title = filter_var($_POST['Phone-title'], FILTER_SANITIZE_STRING);
$Phone_Price = filter_var($_POST['Phone-price'], FILTER_SANITIZE_STRING);
$Phone_Quantity = filter_var($_POST['quantity'], FILTER_SANITIZE_NUMBER_INT);
$delete_phone = filter_var($_POST['Deleted-ID'], FILTER_SANITIZE_STRING);
$_SESSION['cart'][] = array(
     'Title' => $Phone_Title,
     'Price' => $Phone_Price,
     'quantity' => $Phone_Quantity
);
unset($_SESSION['cart'][$delete_phone]);
var_dump($_SESSION['cart']);

1 个答案:

答案 0 :(得分:0)

您未在数组

中指定 $ id
Role