PDA

View Full Version : Reset Array Key Value


macmonkey
10-02-2006, 08:50 PM
I've got a small shopping cart that has items stored in an Array.

Array ( [0] => Array ([0]=> 1234 [1] => 4 [2] => 3) [1] => Array ( [0] => 2640 [1] => 9 [2] => 1 ) [2] => Array ( [0] => 2885 [1] => 20 [2] => 1 ) )

My problem is that when I print the order out in the shopping cart I use a foreach loop and simply post the "remove" link to a query string with an incremented value 0,1,2,3 etc...

When someone clicks to remove the very first item it removes the first array item but leaves the rest:

Array ( [1] => Array ( [0] => 2640 [1] => 9 [2] => 1 ) [2] => Array ( [0] => 2885 [1] => 20 [2] => 1 ) )

Now my remove buttons don't work because the incremented variable is off.

Any ideas on how to chage this new array so that the first item is associated with [0] instead of [1]

Thanks!

Fumigator
10-02-2006, 11:11 PM
When an item is removed, does the page get reloaded? If it does, then why would the array and the buttons not match anymore? Isn't the foreach loop getting run again and re-assigning the incremented values to the buttons?

At any rate, you could solve your problem by using a unique ID for each item instead of an incremented value.