Remove an item from a PHP array

Given an array in PHP, I need to remove a specific element (if it exists in the array) from the array. The following is a pretty simple and elegant solution. I didn't use unset on purpose because you have to check for a integer result before you remove the item from the array.

$arr     = array('apple','banana','orange');
$new_arr = array_diff($arr, array('banana'));

print_r($new_arr);
Leave A Reply
All content licensed under the Creative Commons License