Giddy Rob Posted May 27, 2008 Share Posted May 27, 2008 Hi, i am creating a shopping cart and am wondering how I can update a value in the array? The reason I want to do this is for the quantity of items the customer would like to buy. As you can see from the code: $disp="SELECT id, name, description, priceforpaypal, colour FROM portfolio WHERE id='{$_POST['id']}'"; $display=mysql_query($disp); $displays=mysql_fetch_array($display); if(empty($_SESSION['items'])){ $_SESSION['items']=array(array($displays[0],$displays[1],$displays[2],$displays[3],$displays[4],"1")); }//end if The item is added to the session array with the quantity of 1 being added at the end. I have managed to do a search so that it knows if there is a item already in the array with the same id I just don't know what to do to update that record so that the quantity can be changed i.e. +1 Cheers in advance guys Rob Link to comment https://forums.phpfreaks.com/topic/107418-updating-a-value-in-a-multidimensional-array/ Share on other sites More sharing options...
sasa Posted May 27, 2008 Share Posted May 27, 2008 try <?php // setup $id $added = false; foreach ($_SESSION['items'] as $key => $item){ if (in_array($id, $item)){ $_SESSION['items'][$key][5]++; $added = true; } if ($added) break; } if (!$added) // add new session items ?> Link to comment https://forums.phpfreaks.com/topic/107418-updating-a-value-in-a-multidimensional-array/#findComment-550776 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.