Giddy Rob Posted May 23, 2008 Share Posted May 23, 2008 Hi, I am creating a shopping cart for the first time and I would like to store information for each item added to the shopping cart in a session variable. I have got as far as creating a multidimensional array to store the information that needs sending to the checkout for the item, but I am a little stuck on how to add to this array when another item is added to the cart. The code I am using to store the array is as follows: $items=array(array($displays[0],$displays[1],$displays[2],$displays[3],$displays[4])); How do I add another item to the items array? I know how to do it all in one go but the customers will go to different pages so it needs to get the array from the session variable and add another item. The whole thing will be stored as a session variable again. Also, as this is the first time I have done this I am kinda making it up as I go along so is it possible for me to store lets say five items in the items array in a session variable then pull out all the information from that session variable when i need it? Main question is am i trying something that is possible? if not what is the best way to execute what I am trying to do? Any help would be great Cheers in advance. Rob Quote Link to comment https://forums.phpfreaks.com/topic/106919-using-an-array-in-session-variables/ Share on other sites More sharing options...
Giddy Rob Posted May 23, 2008 Author Share Posted May 23, 2008 right, i have changed my code to: $_SESSION['items']=array(array($displays[0],$displays[1],$displays[2],$displays[3],$displays[4])); echo $_SESSION['animals'][0][1]; I just need to know how to add another item into that array when the user decides to add a new item. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/106919-using-an-array-in-session-variables/#findComment-547983 Share on other sites More sharing options...
Giddy Rob Posted May 23, 2008 Author Share Posted May 23, 2008 echo $_SESSION['items'][0][1]; //even Quote Link to comment https://forums.phpfreaks.com/topic/106919-using-an-array-in-session-variables/#findComment-547984 Share on other sites More sharing options...
thebadbad Posted May 23, 2008 Share Posted May 23, 2008 To add an item to an array: 1-dimensional: <?php $array = array('first', 'second', 'third'); // add a "fourth" value to the end of the array $array[] = 'fourth'; // $array is now array('first', 'second', 'third', 'fourth') ?> 2-dimensional: <?php $array = array(array('first', 'second', 'third'), array('a', 'b', 'c')); // add a "fourth" value to the end of the first second-level array $array[0][] = 'fourth'; // and to the second $array[1][] = 'd'; // $array is now array(array('first', 'second', 'third', 'fourth'), array('a', 'b', 'c', 'd')) ?> Hope that helps! Quote Link to comment https://forums.phpfreaks.com/topic/106919-using-an-array-in-session-variables/#findComment-547988 Share on other sites More sharing options...
Giddy Rob Posted May 23, 2008 Author Share Posted May 23, 2008 thanks for the help but I need to have for example: $_SESSION['items']=array(array(1,2,3,4,5)); $_SESSION['items']=array(array(6,7,8,9,10)); So that if I do: echo $_SESSION['items'][0][0]; The output will be 1, and if I do: echo $_SESSION['items'][1][0]; The output will be 6 i know this doesnt work but I am not sure how to do it. Hope that makes a bit more sense Quote Link to comment https://forums.phpfreaks.com/topic/106919-using-an-array-in-session-variables/#findComment-547998 Share on other sites More sharing options...
Giddy Rob Posted May 23, 2008 Author Share Posted May 23, 2008 cheers, figured it out from your code $_SESSION['items'][] = array($displays[0],$displays[1],$displays[2],$displays[3],$displays[4]); slaps a new item in the array Rock and roll! Thanks for the prod in the right direction Quote Link to comment https://forums.phpfreaks.com/topic/106919-using-an-array-in-session-variables/#findComment-548002 Share on other sites More sharing options...
thebadbad Posted May 23, 2008 Share Posted May 23, 2008 Good! Quote Link to comment https://forums.phpfreaks.com/topic/106919-using-an-array-in-session-variables/#findComment-548006 Share on other sites More sharing options...
Giddy Rob Posted May 23, 2008 Author Share Posted May 23, 2008 just one last thing. How do I loop around the array so I can display each entry? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/106919-using-an-array-in-session-variables/#findComment-548088 Share on other sites More sharing options...
.josh Posted May 23, 2008 Share Posted May 23, 2008 You have to use nested for() or foreach() loops. example: for ($x = 0; $x < 10; $x++) { for ($y = 0; $y < 10; $y++) { echo $blah[$x][$y] . "<br>"; } // end y } // end x Quote Link to comment https://forums.phpfreaks.com/topic/106919-using-an-array-in-session-variables/#findComment-548102 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.