PHP: How to remove specific element from an array? code example

Example 1: php remove specific element from array

if (($key = array_search('strawberry', $array)) !== false) {
    unset($array[$key]);
}

Example 2: PHP: How to remove specific element from an array?

if (($key = array_search('strawberry', $array)) !== false) {
    unset($array[$key]);
}

Example 3: PHP: How to remove specific element from an array?

$array = array('apple', 'orange', 'strawberry', 'blueberry', 'kiwi', 'strawberry'); //throw in another 'strawberry' to demonstrate that it removes multiple instances of the string
$array_without_strawberries = array_diff($array, array('strawberry'));
print_r($array_without_strawberries);

Tags:

Php Example