This may seem like a simple problem, but for some reason I’m having a brain fart and just can’t wrap my head around it.
I’m working on a very simple cart project and am using an array of structures in the session scope:
Each element in the array is a struct, as I mentioned:
If someone wants to remove an item from their cart, they can set the quantity to 0. Upon submission I loop through the array, checking for val(form.quantity_n). If it returns false, I do arrayDeleteAt(session.cart,n)
The problem arises if I loop through the array using arrayLen(session.cart), which is now one less than arrayLen() was when the loop started, since arrayDeleteAT() pulls the item out of the array and renumbers everything.
In other words, if there were three elements in the array, the loop wants to go from 1 to 3, but since element 1 was removed, the arrayLen is now only 2, total, and the loop throws an error because element 3 no longer exists.
Perhaps I should just create a new array of structures as I loop through, copying over the stay-in-cart items, and then destroying and recreating the cart array from the temp array?
What do other people do?