Yes you can, the easiest way would be to search for the item ID in the array, and then unset the key associated with it -- and then as an extension you could re-base the array to fix the array keys. Please Note: I haven't put any validation on the $arrayKey
and what it returns, you should ensure that the array_search
function is returning as expected before unsetting the array key just to be safe.
So something like this:
$data = [1967, 1966, 2233, 2687, 2081];
$arrayKey = array_search(1966, $data);
unset($data[$arrayKey]);
The specified product ID would be unset by that, just replace 1966 with your $_GET
variable. i.e.
array_search($_GET['delete'], $data)
And the $data
array would be a list of all your valid product IDs, that you could pull out from your database, or wherever you are storing them.
If you want to re-base the key indexes after this you can do:
$data = array_values($data);
What the above does is fixes the array key indexes, when you unset an element it will be removed from the array, but all keys will maintain their current indexes. So the array indexes may progress like so: 0, 1, 3, 4
. If you re-base the array with the above they'll progress naturally again: 0, 1, 2, 3
.
Hope that helps, any questions just let me know.