Here is the array:
[cart] => Array
(
[ProductId] => Array
(
[0] => P121100001
[1] => P121100002
)
[SellerId] => Array
(
[0] => S12110001
[1] => S12110001
)
[SpecifyId] => Array
(
[0] => 1
[1] => 2
)
[Quantity] => Array
(
[0] => 1
[1] => 1
)
[Price] => Array
(
[0] => 12
[1] => 29
)
[TotalPrice] => 41
)
I have the ProductId and I want to remove all the other items matching P121100002’s key.
Is there an easy way to do this I can’t can seem to come up with one?
You can loop through the full array and use
unset()to, well, “unset” the specified index:The slight caveat to this approach is that it may disrupt your index orders. For instance, say you have:
And you want to remove
P121100002, which has a corresponding index of1. Usingunset($cart['ProductId'][1])will cause your array’s to become:This may be something to remain concerned with if you’re going to use a
forloop to iterate through in the future. If it is, you can usearray_values()to “reset” the indexes in theunset()loop from above: