How to delete array entry by value with PHP?

Spread the love

Sometimes, we want to delete array entry by value with PHP.

In this article, we’ll look at how to delete array entry by value with PHP.

How to delete array entry by value with PHP?

To delete array entry by value with PHP, we can use the array_search function to find the entry to delete.

For instance, we write

if (($key = array_search($del_val, $messages)) !== false) {
    unset($messages[$key]);
}

to search for the key of the entry with value $del_val in the $messages array.

If $key isn’t false, then we call unset with $messages[$key] to remove the found entry.

Conclusion

To delete array entry by value with PHP, we can use the array_search function to find the entry to delete.

Leave a Reply

Your email address will not be published.