I have a PHP array as follows:
$messages = [312, 401, 1599, 3, ...];
I want to delete the element containing the value $del_val (for example, $del_val=401), but I don't know its key. This might help: each value can only be there once.
I'm looking for the simplest function to perform this task, please.
Best Solution
Using
array_search()andunset, try the following:array_search()returns the key of the element it finds, which can be used to remove that element from the original array usingunset(). It will returnFALSEon failure, however it can return a false-y value on success (your key may be0for example), which is why the strict comparison!==operator is used.The
if()statement will check whetherarray_search()returned a value, and will only perform an action if it did.