购物车使用&_SEESION和多维数组

i am making a shop cart using $_SESSION, my problem is the first data in the array is being overwritten by the new one can you help me with this?

here's my code:

    session_start();
    $_SESSION['cart']= array();
    if(isset($_POST['addtocart'])){
                $newproduct= array(
                'code' => $_SESSION['code'],
                'color' => $_POST['color'],
                'size' =>$_POST['size'],
                'quantity' => 1);
                $_SESSION['cart'][]= $newproduct;

    }

Retrieving the values:

session_start();
foreach ( $_SESSION['cart'] AS $item ) 
{  
echo 'code: ' . $item['code'] . '<br />'; 
echo 'color: ' . $item['color'];  
}

You redeclare the cart session variable eah time

$_SESSION['cart']= array();

perhaps you should try:

if( !isset( $_SESSION['cart'] ) ) $_SESSION['cart']= array();

ie:

<?php

    session_start();
    if( !isset( $_SESSION['cart'] ) ) $_SESSION['cart']= array();

    if( isset( $_POST['addtocart'], $_POST['color'],$_POST['size'],$_SESSION['code'] ) ){
        $newproduct=array(
            'code'      =>  $_SESSION['code'],
            'color'     =>  $_POST['color'],
            'size'      =>  $_POST['size'],
            'quantity'  =>  1
        );
        $_SESSION['cart'][]= $newproduct;
    }

?>