PHP array delete by value (not key)

前端 未结 19 1775
花落未央
花落未央 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
    
    0 讨论(0)
提交回复
热议问题