私が作成しているショッピングカートは、配列の最初の要素の数量を更新しているようです。たとえば、ショッピングカートの最初のアイテムの数量は1で、商品ページからさらに2の数量を追加すると、合計が3に変わります。これは私が望むものです。ただし、別のアイテムに対してこれらの手順を繰り返すと、グループ化するのではなく、個別に配列に追加されます。
if(isset($_GET['add'])){
foreach ($_SESSION['cart'] as $key => $item){
if ($item['id'] == $itemID) {
$newQuan = $item['quantity'] + $quantity;
unset($_SESSION['cart'][$key]);
$_SESSION['cart'][] = array("id" => $itemID,"quantity" => $newQuan);
header('Location:xxx');//stops user contsanlty adding on refresh
exit;
}
else{
$_SESSION['cart'][] = array("id" => $itemID,"quantity" => $quantity);
header('xxx');//stops user contsanlty adding on refresh
exit;
}
}
}
最初の要素だけが更新される理由について誰かが私を助けてくれますか?