tags:

views:

45

answers:

3

Lets say I have this array:

$queue = array("orange", "banana", 'apple', 'watermelon');

If I want to remove any of them,for example I want to remove banana, how to do it?

+1  A: 

You'll need to search for that element and remove it using the key:

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

If the array can contain the value more than once, you should use array_keys() instead:

foreach (array_keys($array, 'banana') as $key) {
    unset($array[$key]);
}
soulmerge
+4  A: 
if (in_array('banana', $array)) 
{
    unset($array[array_search('banana', $array)]);
}
John Conde
It should be `unset($array[array_search('banana', $array)]);` on line 3
jwandborg
Oops. Typed too fast. Nice catch. +1
John Conde
A: 

with array_filter:

$array = array_filter($array, create_function('$v', 'return $v != \'value to remove\';'));
knittl
As of PHP 5.3 you don't need to use create_function, you can just write an anonymous one inline.<br>`function($v) { return $v != 'value to remove'; }`
Greg K
yes, but `create_function` works in older versions too
knittl