aebstract Posted February 2, 2009 Share Posted February 2, 2009 Just want some input on your thoughts for an array structure. Right now, for a shopping cart, each item is added as it's own id to a session and then split up in to an array. So 4,4,4,5,3. Then after splitting that up a foreach ($contents as $id=>$qty) { will tell us that we have qty 3 of item 4, 1 of 5 and 1 of 3. Would it be better to somehow make this a deeper array, something like: array('item1' => array('1', '1', '1'), 'item2' => array('1', '1')); Then I can display item 1 and know I have 3 of it and item2 I have 2 of it? If so how do I go about storing something like this in a variable and how do I go about grabbing those results? Quote Link to comment https://forums.phpfreaks.com/topic/143526-solved-array-structure-question/ Share on other sites More sharing options...
premiso Posted February 2, 2009 Share Posted February 2, 2009 What would be the advantage of doing that? Having it as $itemid => $qty should be just fine. Quote Link to comment https://forums.phpfreaks.com/topic/143526-solved-array-structure-question/#findComment-752915 Share on other sites More sharing options...
Psycho Posted February 2, 2009 Share Posted February 2, 2009 Why not just have a quantity for each item: array( //ID => Qty '3' => 1, '4' => 3, '5' => 1 ) EDIT: Permiso beat me to it Quote Link to comment https://forums.phpfreaks.com/topic/143526-solved-array-structure-question/#findComment-752916 Share on other sites More sharing options...
aebstract Posted February 2, 2009 Author Share Posted February 2, 2009 Well if I have 3 of the same item and want to update it to 10, or 1... I'm having trouble figuring out how to easily update my quantity because of the way it is listed. edit: I see what you mean but I am pretty stupid with arrays, gonna fiddle around and see if I can figure it out. Quote Link to comment https://forums.phpfreaks.com/topic/143526-solved-array-structure-question/#findComment-752917 Share on other sites More sharing options...
Psycho Posted February 2, 2009 Share Posted February 2, 2009 Well if I have 3 of the same item and want to update it to 10, or 1... I'm having trouble figuring out how to easily update my quantity because of the way it is listed. If the current quantity is 3 and you want to change it to 10, then simply update the value to 10. Current cart: $cart = array( //ID => Qty '3' => 1, '4' => 3, '5' => 1 ) Then if you need to change the quantity for product with the ID of 3 you would simply state $cart[3] = 10; Quote Link to comment https://forums.phpfreaks.com/topic/143526-solved-array-structure-question/#findComment-752918 Share on other sites More sharing options...
aebstract Posted February 2, 2009 Author Share Posted February 2, 2009 My current array when printed looks like this: array(7) { [0]=> string(1) "8" [1]=> string(1) "8" [2]=> string(6) "11;4.2" [3]=> string(1) "7" [4]=> string(6) "11;4.3" [5]=> string(6) "11;4.2" [6]=> string(6) "11;4.2" } Needs to be formatted something like array ( '11;4.2' => 3, '11;4.2' => 3, '11;4.2' => 3, '8' => 2, '7' => 1 ) Though I'm not sure how to set it up to work like that. Quote Link to comment https://forums.phpfreaks.com/topic/143526-solved-array-structure-question/#findComment-752924 Share on other sites More sharing options...
Psycho Posted February 2, 2009 Share Posted February 2, 2009 Well, you have to create it like that. I'm assuming you have code already developed that creates the array when someone adds an item to the cart and it always adds a new element to the array. You will need to modify that code so it increments any existing elements. Simple example for a function to add a single qty to an item in the cart $cart = array(); function addItem($itemID) { global $cart; if(!isset($cart[$itemID])) { $cart[$itemID] = 1; return; } $cart[$itemID]++; } Again, that is a "simple" example. A real solution would probably allow for the value to be set to a fixed qty or to increment/decrement the quantity as needed. It all depends on your particular needs. Quote Link to comment https://forums.phpfreaks.com/topic/143526-solved-array-structure-question/#findComment-752933 Share on other sites More sharing options...
aebstract Posted February 2, 2009 Author Share Posted February 2, 2009 Okay, I'm gonna work with that in the morning. Currently all I am doing to make the cart is add the id of the item on to the end of a string. So I have $_SESSION['cart'] = "4,4,4,4"; and just if I wanted to add item 3 it would just get added to the end: 4,4,4,4,3. This leaves me pretty much option-less to do anything else ;( Quote Link to comment https://forums.phpfreaks.com/topic/143526-solved-array-structure-question/#findComment-752935 Share on other sites More sharing options...
Psycho Posted February 2, 2009 Share Posted February 2, 2009 Right. So instead use this structure: $_SESSION['cart'][ItemID] = Quantity; $_SESSION['cart'][4] = 4; $_SESSION['cart'][3] = 1; Quote Link to comment https://forums.phpfreaks.com/topic/143526-solved-array-structure-question/#findComment-753033 Share on other sites More sharing options...
aebstract Posted February 3, 2009 Author Share Posted February 3, 2009 Right. So instead use this structure: $_SESSION['cart'][ItemID] = Quantity; $_SESSION['cart'][4] = 4; $_SESSION['cart'][3] = 1; So something like, $_SESSION['cart'][$specialid] = 1; right? With that, how would I work with the id and qty for each item? Like how can I set em as a variable or something? Quote Link to comment https://forums.phpfreaks.com/topic/143526-solved-array-structure-question/#findComment-753478 Share on other sites More sharing options...
aebstract Posted February 3, 2009 Author Share Posted February 3, 2009 $_SESSION['cart'] = array($specialid => '1'); That should work, correct? I just don't know how to echo out the $_SESSION['cart'] Quote Link to comment https://forums.phpfreaks.com/topic/143526-solved-array-structure-question/#findComment-753494 Share on other sites More sharing options...
aebstract Posted February 3, 2009 Author Share Posted February 3, 2009 I think this is solved for now, being able to: foreach ($_SESSION['cart'] as $id=>$qty){ print "$id qty of $qty"; } Quote Link to comment https://forums.phpfreaks.com/topic/143526-solved-array-structure-question/#findComment-753502 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.