Delete element by value from array (not by key)

While working with array in PHP sometimes we need to check if a value exists in an array and delete that key and value together from array. This simple code snippet below with explanation will help you to understand how can you delete element by value from array (not by key).

Delete element by value from array

PHP array_search() and unset() functions are handy in such task.

The array_search() returns the key of element to find and unset() will remove the element using the key returned, it returns FALSE if value is not found in array and we need a strict comparison !==  because having key as zero can return a falsey value on success.

It’s the simple way to delete element by value from array (not by key).

You Might Interested In

Leave a Reply

Enclose a code block like: <pre><code>Your Code Snippet</code></pre>.