PHP array delete by value (not key)

前端 未结 19 1785
花落未央
花落未央 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: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));
    

提交回复
热议问题