PHP array delete by value (not key)

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

    Or simply, manual way:

    foreach ($array as $key => $value){
        if ($value == $target_value) {
            unset($array[$key]);
        }
    }
    

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

提交回复
热议问题