hi, im on a shopping cart system at the moment, and i want the shoping basket saved in tn the session, until the user enters his adress and hits "confirm order". (so i dont have to create an extra table with temporary orders and clean it up from time to time). i tried something like $cart=array("article_id"=>123, "pieces"=>4) but im stuck here because i have no idea how to save this array in the $_SESSION[] (already an array), and how to add/change articles later. maybe its just to early in the morning - my eyes are open but my brain is still sleeping...
function add_to_cart($qty, $productID}{ $BASKET =array(); $BASKET =$_SESSION['Basket']; $_SESSION['NumItems']=$_SESSION['NumItems']+$qty; if(array_key_exists($productID,$BASKET)){ $BASKET["$productID"]=$BASKET["$productID"]+$qty; //Load the $basket array into the session array under Basket $_SESSION['Basket']=$BASKET; }else{ $BASKET["$productID"] =$productID; $BASKET["$productID"] =$qty; //Load the $basket array into the session array under Basket $_SESSION['Basket']=$BASKET; }; } Code (markup): Bit of an old one that so bit messy but you should get the grasp of it if not can provide a better coded/comment or sure someone else will
If it's a simple key => value array, I like to implode the array into a string: $arraystring = implode("|", $array); // array becomes "item1|item2|item3|item4| etc..." Code (markup): and then use explode("|", $arraystring) to make the string an array again. For multidimensional arrays (i.e. key => value1, value2, etc.) I like to use serialize($array) as it preserves the array. Then just unserialize() when I need the array again Once either function is performed, the array (now a string) can be saved as a single value (i.e. $_SESSION['myarraystring'] ).