I want to use array_pop to remove the last item added to the SESSION array. So it would remove the REQUEST item. The problem is that nothing is left in the session array because I pop off the last item each time that the form is submitted. Maybe I need to use array_push to push the popped items back on to the SESSION array after the logic has been applied. I don’t understand how array_pop is supposed to work on a SESSION array. Can anyone explain to me what behaviour to expect? I expected to get a SESSION array with the REQUEST item removed but I thought that I would still have all of my SESSION items.
It would be helpful if you could describe the real problem you are trying to solve by doing this.
yeah then… just don’t do that? If you have a specific item that can be determined by a unique key, then why are you even using array_pop instead of unset()?
I want to remove the newly submitted item, the REQUEST item from the SESSION array so that I can compare the SESSION array items with the REQUEST item and see if there are any duplicates. It’s for a shopping cart application so if a duplicate product is found, the quantities for the two same products are added and only one product is shown. I have no idea how to do this and my code is a mess.
Your session cart array should use the item id as the array index and just store the quantity as the array value under the index. This will simplify all your code. You can then directly test/reference items in the cart using the item id.
I need to rewrite the entire program so that I’m using a multidimensional array instead of five separate arrays and then it will probably be easier to merge the two products with the same name and add their quantities.
It sounds like you are thinking of passing more than just the item id and the quantity through any cart related form. If so, this is overly complicated and a huge security problem, since all external data can be manipulated and changed to be anything. The only two values that should be submitted by the form are the item id and the quantity. All other product values can be displayed on the web page, but when you need them on the server, retrieve them from where they are stored, Keep It Simple (KISS.)
I am using SESSIONS without a database. It sounds like you are suggesting that I retrieve the product values from a database. I have a json file with the product values but I don’t know how to retrieve them without using SESSIONS.
It doesn’t matter where the product information is stored. The cart operations -inserting a quantity of a new product, updating the quantity of an existing product, or deleting a product, needs to be simple and straight-forward, which is what using the array structure I described will help accomplish.