Remove array element by value in PHP
With collection types like those provided in Doctrine you have methods like removeElement()
. When using plain arrays we unfortunately don't have those useful helper functions.
What we can use though is a combination of array_search
and unset
:
if (($key = array_search($id, $arrayToClean, true)) !== false) {
unset($arrayToClean[$key]);
}
Of course you can also move this into a helper method of your own.