PHP array delete by value (not key)

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

    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 instantly find the value. Better than using searching in all elements over and over again.

    $messages=array();   
    $messages[312] = 312;    
    $messages[401] = 401;   
    $messages[1599] = 1599;   
    $messages[3] = 3;    
    
    unset($messages[3]); // no search needed
    

提交回复
热议问题