Showing posts with label array_search. Show all posts
Showing posts with label array_search. Show all posts

Friday, March 1, 2013

Friday, February 1, 2013

php array delete by value (not key)

Using array_seach(), try the following:
if(($key = array_search($del_val, $messages)) !== false) {
    unset($messages[$key]);
}

array_search() returns the key of the element it finds, which can be used to remove that element from the original array using unset(). It will return FALSE on failure, however it can return a "falsey" value on success (your key may be 0 for example), which is why the strict comparison !== operator is used.
The if() statement will check whether array_search() returned a value, and will only perform an action if it did.