need advice on comparing values of nested arrays.
Posted: Thu Mar 29, 2007 4:29 pm
a little background:
i'm building a pseudo-shopping cart for a client of mine. i say 'pseudo' because there is no database or cms backend for this system. all the items will be hard-coded in HTML forms. the shopping cart items will be stored entirely in a session as any normal shopping cart handles them.
each item will have a few pre-defined values such as id, price, name, quantity, and an array called itemInfo. for example, let's say one of the item's is a sandwitch. the sandwitch comes standard with lettuce, tomatoes, and cheese. these options will be the pre-checked options in the sandwitch form. however, the user can uncheck these options, and check other options such as 'mayonaise' or 'pickles' for example. so imagine these variables are stored in hidden input fields for the sandwitch form:
now here's where it gets confusing for me... i need to write a function to check all the items of the shopping cart to FIRST see if there is a repeat of the same item in the shopping cart (for instance if the user added the sandwitch twice), THEN if there is more than one of the same item in the shopping cart, compare their $itemInfo array to see if they are the same... if they are, delete one of the two items, and add 1 to the quantity of the first item.
the reason why i need this is because there shouldn't be 2 separate sandwitches listed in the shopping cart, if they are the same sandwitch. it should only have one entry, and show a quantity of 2. but the user can add 2 sandwitches, but have different things on the sandwitch, in which case there would be 2 separate sandwitches in the cart and their contents listed below each one.
does that make sense? if there is any more information you guys need to know, just let me know. i'll do my best to explain the situation as detailed as possible so i can be led in the right direction.
i haven't begun coding, yet, i want to get the logic down before hand. i'm sure i'm going to be using array_diff_assoc and while loops and foreach loops, but i just don't know how and in what order. i'm not looking for anyone to code it for me, just a little help on the logistics. thanks.
EDIT:
just a little bit more info on the situation. the contents of the cart will be stored in this manner:
but if there are two sandwitches with the same exact itemInfo:
does that make sense?
i'm building a pseudo-shopping cart for a client of mine. i say 'pseudo' because there is no database or cms backend for this system. all the items will be hard-coded in HTML forms. the shopping cart items will be stored entirely in a session as any normal shopping cart handles them.
each item will have a few pre-defined values such as id, price, name, quantity, and an array called itemInfo. for example, let's say one of the item's is a sandwitch. the sandwitch comes standard with lettuce, tomatoes, and cheese. these options will be the pre-checked options in the sandwitch form. however, the user can uncheck these options, and check other options such as 'mayonaise' or 'pickles' for example. so imagine these variables are stored in hidden input fields for the sandwitch form:
Code: Select all
$id = 1;
$name = 'sandwitch';
$price = 6.50;
$quantity = 1;
$itemInfo = array('23' => 'lettuce', '12' => 'tomatoes', '43' => 'mayonaise');the reason why i need this is because there shouldn't be 2 separate sandwitches listed in the shopping cart, if they are the same sandwitch. it should only have one entry, and show a quantity of 2. but the user can add 2 sandwitches, but have different things on the sandwitch, in which case there would be 2 separate sandwitches in the cart and their contents listed below each one.
does that make sense? if there is any more information you guys need to know, just let me know. i'll do my best to explain the situation as detailed as possible so i can be led in the right direction.
i haven't begun coding, yet, i want to get the logic down before hand. i'm sure i'm going to be using array_diff_assoc and while loops and foreach loops, but i just don't know how and in what order. i'm not looking for anyone to code it for me, just a little help on the logistics. thanks.
EDIT:
just a little bit more info on the situation. the contents of the cart will be stored in this manner:
Code: Select all
$_SESSION['cart']
|
+----[item1]
| |
| +-----[id] = 1
| |
| +-----[name] = 'sandwitch'
| |
| +-----[price] = 6.50
| |
| +-----[quantity] = 1
| |
| +-----[itemInfo]
| |
| +-----[lettuce]
| |
| +-----[tomatoes]
| |
| +-----[cheese]
|
+----[item2]
| |
| +-----[id] = 1
| |
| +-----[name] = 'sandwitch'
| |
| +-----[price] = 6.50
| |
| +-----[quantity] = 1
| |
| +-----[itemInfo]
| |
| +-----[lettuce]
| |
| +-----[tomatoes]
| |
| +-----[cheese]
| |
| +-----[mayonaise]
|
+----[item3]
|
+-----[id] = 1
|
+-----[name] = 'sandwitch'
|
+-----[price] = 6.50
|
+-----[quantity] = 1
|
+-----[itemInfo]
|
+-----[lettuce]
|
+-----[mustard]
|
+-----[cheese]Code: Select all
$_SESSION['cart']
|
+----[item1]
| |
| +-----[id] = 1
| |
| +-----[name] = 'sandwitch'
| |
| +-----[price] = 6.50
| |
| +-----[quantity] = 2
| |
| +-----[itemInfo]
| |
| +-----[lettuce]
| |
| +-----[tomatoes]
| |
| +-----[cheese]
|
+----[item2]
|
+-----[id] = 1
|
+-----[name] = 'sandwitch'
|
+-----[price] = 6.50
|
+-----[quantity] = 1
|
+-----[itemInfo]
|
+-----[lettuce]
|
+-----[tomatoes]
|
+-----[cheese]
|
+-----[pickles]