PHP array delete by value (not key)

前端 未结 19 2119
花落未央
花落未央 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:07

    The accepted answer converts the array to associative array, so, if you would like to keep it as a non-associative array with the accepted answer, you may have to use array_values too.

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

    The reference is here

提交回复
热议问题