PHP array delete by value (not key)

前端 未结 19 1774
花落未央
花落未央 2020-11-22 15:58

I have a PHP array as follows:

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

I want to delete the element containing the value $del_val

相关标签:
19条回答
  • 2020-11-22 16:19

    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 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]).

    0 讨论(0)
  • 2020-11-22 16:20

    Using array_search() and unset, 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 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.

    0 讨论(0)
  • 2020-11-22 16:20

    Well, deleting an element from array is basically just set difference with one element.

    array_diff( [312, 401, 15, 401, 3], [401] ) // removing 401 returns [312, 15, 3]
    

    It generalizes nicely, you can remove as many elements as you like at the same time, if you want.

    Disclaimer: Note that my solution produces a new copy of the array while keeping the old one intact in contrast to the accepted answer which mutates. Pick the one you need.

    0 讨论(0)
  • 2020-11-22 16:20

    With PHP 7.4 using arrow functions:

    $messages = array_filter($messages, fn ($m) => $m != $del_val);
    

    To keep it a non-associative array wrap it with array_values():

    $messages = array_values(array_filter($messages, fn ($m) => $m != $del_val));
    
    0 讨论(0)
  • 2020-11-22 16:20

    A one-liner using the or operator:

    ($key = array_search($del_val, $messages)) !== false or unset($messages[$key]);
    
    0 讨论(0)
  • 2020-11-22 16:21

    I think the simplest way would be to use a function with a foreach loop:

    //This functions deletes the elements of an array $original that are equivalent to the value $del_val
    //The function works by reference, which means that the actual array used as parameter will be modified.
    
    function delete_value(&$original, $del_val)
    {
        //make a copy of the original, to avoid problems of modifying an array that is being currently iterated through
        $copy = $original;
        foreach ($original as $key => $value)
        {
            //for each value evaluate if it is equivalent to the one to be deleted, and if it is capture its key name.
            if($del_val === $value) $del_key[] = $key;
        };
        //If there was a value found, delete all its instances
        if($del_key !== null)
        {
            foreach ($del_key as $dk_i)
            {
                unset($original[$dk_i]);
            };
            //optional reordering of the keys. WARNING: only use it with arrays with numeric indexes!
            /*
            $copy = $original;
            $original = array();
            foreach ($copy as $value) {
                $original[] = $value;
            };
            */
            //the value was found and deleted
            return true;
        };
        //The value was not found, nothing was deleted
        return false;
    };
    
    $original = array(0,1,2,3,4,5,6,7,4);
    $del_val = 4;
    var_dump($original);
    delete_value($original, $del_val);
    var_dump($original);
    

    Output will be:

    array(9) {
      [0]=>
      int(0)
      [1]=>
      int(1)
      [2]=>
      int(2)
      [3]=>
      int(3)
      [4]=>
      int(4)
      [5]=>
      int(5)
      [6]=>
      int(6)
      [7]=>
      int(7)
      [8]=>
      int(4)
    }
    array(7) {
      [0]=>
      int(0)
      [1]=>
      int(1)
      [2]=>
      int(2)
      [3]=>
      int(3)
      [5]=>
      int(5)
      [6]=>
      int(6)
      [7]=>
      int(7)
    }
    
    0 讨论(0)
提交回复
热议问题