Rifts Posted February 18, 2011 Share Posted February 18, 2011 Hey guys I am trying to make a simple shopping cart but I can't seem to wrap my head around this. I have $_SESSION['cart'] as an array and I can add an item to it and then echo specific elements of the item like so echo $_SESSION['cart']['0']['Name']; I can not seem to figure out how to add a second item to the array so I can do something like echo $_SESSION['cart']['1']['Name']; and it will echo the second item that I 'added' to my cart? Thanks I hope that makes sense. Quote Link to comment https://forums.phpfreaks.com/topic/228066-multidimensional-array-cant-comprehend/ Share on other sites More sharing options...
denno020 Posted February 18, 2011 Share Posted February 18, 2011 Here is part 1 of a series of youtube videos where the guy goes through everything you need to create a shopping cart. He uses multi arrays too. I have learnt all my php from this dude, he's very good. You can watch the whole series and get a firm grasp on everything, or you can just find the video where he talks about the multi arrays . Enjoy Denno Quote Link to comment https://forums.phpfreaks.com/topic/228066-multidimensional-array-cant-comprehend/#findComment-1176042 Share on other sites More sharing options...
wigpip Posted February 18, 2011 Share Posted February 18, 2011 the multidimensional array ah yes $_SESSION['cart'] = array(); $_SESSION['cart']['0']['Name'] = 'product 1'; $_SESSION['cart']['1']['Name'] = 'product 2'; $nextnum = count($_SESSION['cart']) + 1; $_SESSION['cart'][$nextnum]['Name'] = 'product 3'; print_r($_SESSION['cart']); so by using the count function you always know where you are up to in the array, then add 1 for the next 'key' to assign it's 'value' Quote Link to comment https://forums.phpfreaks.com/topic/228066-multidimensional-array-cant-comprehend/#findComment-1176085 Share on other sites More sharing options...
Rifts Posted February 18, 2011 Author Share Posted February 18, 2011 ahhhhh I see that is really helpful thank you both! Quote Link to comment https://forums.phpfreaks.com/topic/228066-multidimensional-array-cant-comprehend/#findComment-1176225 Share on other sites More sharing options...
Rifts Posted February 18, 2011 Author Share Posted February 18, 2011 Hey so i'm not sure what I'm doing wrong here. for some reason when I try to add a new product the $nextnum var wont increase so it just keeps overwritting the first product. I have this and then a form to "add to cart" or submit it via post <?php session_start(); $_SESSION['cart'] = array(); $nextnum = count($_SESSION['cart']) + 1; echo $nextnum; $_SESSION['cart'][$nextnum]['name'] = $_POST['name']; $_SESSION['cart'][$nextnum]['id'] = $_POST['id']; $_SESSION['cart'][$nextnum]['price'] = $_POST['price']; $_SESSION['cart'][$nextnum]['size'] = $_POST['size']; $_SESSION['cart'][$nextnum]['color'] = $_POST['color']; print_r($_SESSION['cart']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/228066-multidimensional-array-cant-comprehend/#findComment-1176243 Share on other sites More sharing options...
BlueSkyIS Posted February 18, 2011 Share Posted February 18, 2011 $_SESSION['cart'][$nextnum]['name'][] = $_POST['name']; or $_SESSION['cart'][$nextnum]['name'][$nextnum] = $_POST['name']; Quote Link to comment https://forums.phpfreaks.com/topic/228066-multidimensional-array-cant-comprehend/#findComment-1176270 Share on other sites More sharing options...
Rifts Posted February 18, 2011 Author Share Posted February 18, 2011 ok well i figured that out out I have a new question lol I can not seem to get the number of that array for example say there are 3 things in the cart array $count = count($_SESSION['cart']); for($i=0;$i<$count;$i++){ echo $_SESSION['cart'][$i]; } instead of listing 0,1,2 it just says Array,Array,Array why is that? Quote Link to comment https://forums.phpfreaks.com/topic/228066-multidimensional-array-cant-comprehend/#findComment-1176293 Share on other sites More sharing options...
BlueSkyIS Posted February 18, 2011 Share Posted February 18, 2011 because each thing inside $_SESSION['cart'] is an array. when you loop over them and echo them, you get array because that's what they are. Quote Link to comment https://forums.phpfreaks.com/topic/228066-multidimensional-array-cant-comprehend/#findComment-1176296 Share on other sites More sharing options...
Rifts Posted February 18, 2011 Author Share Posted February 18, 2011 ok so what do I need to do to get the spot in the cart array they are? Quote Link to comment https://forums.phpfreaks.com/topic/228066-multidimensional-array-cant-comprehend/#findComment-1176298 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.