数组值被循环中的新值覆盖

I spent a good 30 minutes looking on this site trying to find an answer to this problem however what ever I seem to try won't fix it.

Right at the end of the php code block. I create a session array called "checkout" where I want to store each of the item's ID inside. This is then passed over to a checkout page where the user will see a summary of what they have ordered/their details.

How ever when I come to the checkout page I can only ever seem to get the last $item_id added to the cart to show up

I think each time the loop runs, the $item_id value is being over written inside the "checkout" array instead of being added onto the end of the array. But I'm just not sure how to fix it.

Any help you can give would be greatly appreciated as I've been sat here for 3 hours now trying to get round this problem.

<?php

$cartOutput ="";
$cartTotal = "0";
$totalVat = "0";


if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
$cartOutput = "<h2 align='center'>Your Shopping cart is empty</h2>";
} else {
$i = 0;
foreach ($_SESSION["cart_array"] as $each_item){

    $item_id = $each_item['item_id'];
    $sql = mysql_query("SELECT * FROM estock WHERE stockno='$item_id' LIMIT 1");
    while ($row = mysql_fetch_array($sql)){
        $product_name = $row["description"];
        $price = $row["price"];
        $details = $row["details"];
    }


    $pricetotal = $price * $each_item['quantity'];
    $cartTotal = $pricetotal + $cartTotal;
    $totalVat = $cartTotal * 1.175;
    //Table 
    $cartOutput .= '<tr>';
    $cartOutput .= '<td> <a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name . '" width="40" height="52" border="1" />  </td>';
    $cartOutput .= '<td>'.$details.'</td>';
    $cartOutput .= '<td>£'.$price.'</td>';
    $cartOutput .= '<td>'.$each_item['quantity'].'</td>';
    $cartOutput .= '<td>£'.$pricetotal.'</td>';
    $cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>';
    $cartOutput .= '</tr>'; 



    $_SESSION["checkout"] = array();
    $_SESSION["checkout"][] = $item_id;
    $i++;
    if ($i++ > 3) break;

}   

}

?>

Below you can see what I'm doing to retrieve the array on the checkout.php page

I then echo out $cartOutput inside a table.

foreach ($_SESSION["checkout"] as $itemid){
    $cartOutput = "";
    $item_id = $itemid;
    $sql = mysql_query("SELECT * FROM estock WHERE stockno='$item_id' LIMIT 1");
    while ($row = mysql_fetch_array($sql)){
        $item_id = $row["stockno"];
        $product_name = $row["description"];
        $price = $row["price"];
        $details = $row["details"];
    }

    $cartOutput .= '<tr>';
    $cartOutput .= '<td>'.$item_id.'</td>';
    $cartOutput .= '</tr>'; 
}
?>

The problem is in this line

$_SESSION["checkout"] = array();

Put this line out of the main foreach loop and in the loop add the values in the array. Because each time you are creating new array and thats why you old values are lost.