我真的很困惑。它是数组中的数组,例如:
Array ( [0] => Array ( [item] => product1 [unitprice] => 15 [quantity] => 1 ) [1] => Array ( [item] => product2 [unitprice] => 15 [quantity] => 1 ) )
我尝试使用以下方法删除特定项目:
$pid=$_GET['id']; (where id = product1)
$delete=array_splice($_SESSION['cart'], array_search($id, $_SESSION['cart']), 1);
unset($delete);
print_r($_SESSION['cart']);
这似乎是随机删除项目。任何帮助将不胜感激
答案 0 :(得分:2)
<?php
function searchForItem($id, $array) {
foreach ($array as $key => $val) {
if ($val['item'] === $id) {
return $key;
}
}
return null;
}
$pid=$_GET['id'];
$id = searchForItem($pid, $_SESSION['cart']);
unset($_SESSION['cart'][$id]);
?>
答案 1 :(得分:0)
foreach($_SESSION['cart'] as $index => $v) {
if(($key = array_search($pid, $v)) !== false) {
unset($_SESSION['cart'][$index]);
}
}