array_merge will renumber arrays if they have numeric keys. Are your product_codes numeric?
Honestly, all that code is far more complicated than it needs to be. You're checking if the product_code exists in the cart so you can update its quantity, and then you run a loop over everything in the cart? And when you want to add the (new) item to the cart, you use array_merge instead of just adding the item directly to the cart array?
Try spending some time thinking about what's happening in there and whether there's any way you can reduce the number of lines to, say, half of what's there now.