Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a PHP array as follows:

$messages=array();
$messages[1] = 312;
$messages[2] = 401;
$messages[3] = 1599;
$messages[4] = 3;
...

I want to delete the element containing the value $del_val (for example, $del_val=401;), but I don't know its key. This might help: each value can only be there once.

I'm looking for the simplest function to perform this task please.

share|improve this question
If each value only exists once, why not use them as keys instead? Would speed up the removal too :) – Jack Nov 22 '12 at 12:13
1  
@Jack there exist a lot of possibilities where you're retrieving an array like this from a source you don't control (database or whatever). – pnomolos Mar 22 at 20:24

10 Answers

up vote 122 down vote accepted

Using array_search(), try the following:

if(($key = array_search($del_val, $messages)) !== false) {
    unset($messages[$key]);
}

array_search() returns the key of the element it finds, which can be used to remove that element from the original array using unset(). It will return FALSE on failure, however it can return a "falsey" value on success (your key may be 0 for example), which is why the strict comparison !== operator is used.

The if() statement will check whether array_search() returned a value, and will only perform an action if it did.

share|improve this answer
1  
Would $messages = array_diff($messages, array($del_val)) work too? Would it be better in performance? – Adam Strudwick Aug 29 '11 at 0:55
   
@Adam Why not test it out? My feeling is that array_diff() would be slower as it's comparing two arrays, not simply searching through one like array_search(). – Bojangles Aug 29 '11 at 0:57
1  
Even though this is valid, you should avoid assigning values in statements like that. It will only get you into trouble. – adlawson Aug 29 '11 at 1:05
2  
What if $key is 0? – evan Aug 29 '11 at 1:06
1  
If the value you're searching for has a key of 0 or any other falsey value, it won't unset the value and your code won't work. You should test $key === false. (edit- you got it) – evan Aug 29 '11 at 1:11
show 6 more comments

You are loking for simplest function? Well, you won't find anything simpler.

$array=array(312, 401, 1599, 3);
$toDelete=401;

$array=array_diff($array, array($toDelete));
share|improve this answer
2  
this only works for objects that can be converted to a string – nischayn22 Aug 12 '12 at 20:20

One interesting way is by using array_keys():

foreach (array_keys($messages, 401, true) as $key) {
    unset($messages[$key]);
}

The array_keys() function takes two additional parameters to return only keys with a particular value and whether strict checking is required (i.e. using === for comparison).

This can also remove multiple array items with the same value (e.g. [1, 2, 3, 3, 4]).

share|improve this answer
1  
Great answer :) – GuruBob May 13 at 22:05

If you know for definite that your array will contain only one element with that value, you can do

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

If, however, your value might occur more than once in your array, you could do this

$array = array_filter($array, function($e) use ($del_val) {
    return ($e !== $del_val);
});

Note: The second option only works for PHP5.3+ with Closures

share|improve this answer
$fields = array_flip($fields);
unset($fields['myvalue']);
$fields = array_flip($fields);
share|improve this answer

Get the key with array_search().

share|improve this answer
How do I delete the value IF and only if I find it with array_search? – Adam Strudwick Aug 29 '11 at 0:50
$k = array_search($needle, $haystack, true); if ($k !== false) { unset($haystack[$k]); } – evan Aug 29 '11 at 0:56
function array_remove_by_value($array, $value)
{
    return array_values(array_diff($array, array($value)));
}

$array = array(312, 401, 1599, 3);

$newarray = array_remove_by_value($array, 401);

print_r($newarray);

Output

Array ( [0] => 312 [1] => 1599 [2] => 3 )

share|improve this answer

Have a look at following code:

$arr = array('nice_item', 'remove_me', 'another_liked_item', 'remove_me_also');

You can do:

$arr = array_diff($arr, array('remove_me', 'remove_me_also'));

And that will get you this array:

array('nice_item', 'another_liked_item')
share|improve this answer
<?php
if(in_array($del_file_name,$old_files_array,TRUE)){
$after_delete_array     =   array_values(array_diff($old_files_array, array($del_file_name)));
}
?>
share|improve this answer

To delete multiple values try this one

while(($key = array_search($del_val, $messages)) !== false) {
    unset($messages[$key]);
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.