How do I remove an element from an array when I know the elements name? for example:
I have an array:
$array = array('apple', 'orange', 'strawberry', 'blueberry', 'kiwi');
the user enters strawberry
strawberry
is removed.
To fully explain:
I have a database that stores a list of items separated by a comma. The code pulls in the list based on a user choice where that choice is located. So, if they choose strawberry they code pulls in every entry were strawberry is located then converts that to an array using split(). I want to them remove the user chosen items, for this example strawberry, from the array.
This is a simple reiteration that can delete multiple values in the array.
If you are using a plain array here (which seems like the case), you should be using this code instead:
unset($array[$key])
only removes the element but does not reorder the plain array.Supposingly we have an array and use array_splice:
Compared to unset:
Notice how
unset($array[$key])
does not reorder the array.