Home >Backend Development >PHP Tutorial >How to Delete Array Elements by Value in PHP?
Deleting Array Elements by Value in PHP
In PHP, arrays are indexed by keys, typically integers or strings. However, situations may arise where you need to remove an element from an array based on its value rather than its key. This can be achieved by utilizing the array_search() function in conjunction with unset().
Using array_search() and unset()
To delete an array element by value, follow these steps:
Example:
Given an array $messages containing unique values:
$messages = [312, 401, 1599, 3, ...];
To delete the element with the value 401:
if (($key = array_search(401, $messages)) !== false) { unset($messages[$key]); }
Explanation:
Note:
The above is the detailed content of How to Delete Array Elements by Value in PHP?. For more information, please follow other related articles on the PHP Chinese website!