Take the 2-minute tour ×

I have a PHP array as follows:

$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
@Adam Strudwick But if you have many deletions on this array, would it be better to iterate it once and make its key same as value? –  dzona Nov 14 '13 at 9:00

19 Answers 19

up vote 415 down vote accepted

Using array_search() and unset, try the following:

if(($key = array_search($del_val, $messages)) !== false) {

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 false-y 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
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
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
What if $key is 0? –  evan Aug 29 '11 at 1:06
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

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

function array_delete($array, $element) {
    return array_diff($array, [$element]);

array_delete( [312, 401, 1599, 3], 401 ) // returns [312, 1599, 3]

Or in standalone version:

array_diff( [312, 401, 1599, 3], [401] ) // returns [312, 1599, 3]

Disclaimer: This only works for arrays of objects that can be converted to strings.

share|improve this answer
this only works for objects that can be converted to a string –  nischayn22 Aug 12 '12 at 20:20
I seem to be getting a 'Parse Error' for saying [$element], I used array($element) instead. No biggie, but just wanted anyone who had a similar issue to know that they weren't alone –  Angad Aug 26 '13 at 14:11
Sure, I have assumed PHP 5.4 is now in majority to drop the old notation. Thanks for the remark. –  Rok Kralj Aug 26 '13 at 18:57
It's worth noting that for some reason array_diff uses (string) $elem1 === (string) $elem2 as its equality condition, not $elem1 === $elem2 as you might expect. The issue pointed out by @nischayn22 is a consequence of this. If you want something to use as a utility function that will work for arrays of arbitrary elements (which might be objects), Bojangle's answer might be better for this reason. –  Mark Amery Jan 1 at 22:39
Also note that this method performs a sort internally for each argument to array_diff() and thus nudges the runtime up to O(n lg n) from O(n). –  Ja͢ck Jul 14 at 6:12

One interesting way is by using array_keys():

foreach (array_keys($messages, 401, true) as $key) {

The array_keys() function takes two additional parameters to return only keys for 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
Great answer :) –  GuruBob May 13 '13 at 22:05
so much looooooops..... –  blasteralfred Ψ Apr 16 at 17:04
@blasteralfredΨ A linear search is O(n); I'm not sure why you seem to think that it's a problem. –  Ja͢ck Apr 17 at 7:01
Yes, this is effective for selecting multiple array items/keys. –  Oki Erie Rinaldi Aug 28 at 4:52
@Rodniko in which case you would need array_values() as well; the remaining keys are still in the same order though, so technically it's not "unsorted" –  Ja͢ck yesterday

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) {

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);
$fields = array_flip($fields);
share|improve this answer
This only works when your array does not contain duplicate values other than the ones you're trying to remove. –  jberculo Jun 2 at 8:35
@jberculo and sometimes that exactly what you need, in some cases it saves me doing a array unique on it aswel –  DarkMukke Nov 14 at 15:06
Maybe, but I would use functions specifically designed to do that, instead of being just a fortunate side effect of a function that is basically used and intended for something else. It would also make your code less transparent. –  jberculo Nov 14 at 16:08

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

Or simply the good old fasion

    if ($value == $target_value) {

This is the safest of them because you have full control on your array

share|improve this answer
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);



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

share|improve this answer
This solution reorganize the index, good job. –  82din Jan 15 at 22:08

To delete multiple values try this one

share|improve this answer

By the following code, the repetitive values will be removed from the $messages.

$messages = array_diff($messages, array(401));

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

If you don't know its key it means it doesn't matter.

You could place the value as the key, it means it will instant find the value. Better than using searching in all elements over and over again.

$messages[312] = 312;
$messages[401] = 401;
$messages[1599] = 1599;
$messages[3] = 3;

unset($messages[3]); // no search needed

share|improve this answer

If your values you want to delete are, or can, be in an array. Use the array_diff function. Seems to work great for things like this.


$arrayWithValuesRemoved = array_diff($arrayOfData, $arrayOfValuesToRemove);
share|improve this answer

@Bojangles answer did help me. Thank you.

In my case, the array could be associative or not, so I added this function

function test($value, $tab) {

 if(($key = array_search($value, $tab)) !== false) {
    unset($tab[$key]); return true;

 } else if (array_key_exists($value, $tab)){
        unset($tab[$value]); return true;

 } else {
    return false; // the $value is not in the array $tab



share|improve this answer

The Best way is Array Splice

$array = array(14,22,37,42,58,61,73,82,96,10);
array_splice($array, array_search(58, $array ), 1);

Reason for Best is here at http://www.programmerinterview.com/index.php/php-questions/how-to-delete-an-element-from-an-array-in-php/

share|improve this answer
This will not work on associative arrays and arrays that have gaps in their keys, e.g. [1, 2, 4 => 3]. –  Ja͢ck Jul 14 at 6:27
No sorry this will work. Please read the article I have provided link –  Abdul Jabbar WebBestow Jul 15 at 12:05
It won't. Consider the array of my above comment; after I use your code to remove the value 3, the array will be [1, 2, 3]; in other words, the value wasn't removed. To be clear, I'm not saying it fails in all scenarios, just this one. –  Ja͢ck Jul 15 at 12:10

If you have > php5.3, there is the one line code :

$array = array_filter($array, function($i){return $i != $value;}); 
share|improve this answer
share|improve this answer
-1. This would remove $array[0] (if it exists) when $value can't be found. –  Ja͢ck Jul 14 at 6:17

Or a one-liner using the or operator:

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

Another idea to delete a value of an array, use array_diff. If I want to

$my_array = array(1=>"a", "second_value"=>"b", 3=>"c", "d");
$new_array_without_value_c = array_diff($my_array, array("c"));

(Doc : http://php.net/manual/fr/function.array-diff.php)

share|improve this answer

protected by Ja͢ck Jul 14 at 6:14


Would you like to answer one of these unanswered questions instead?
