PHP array delete by value (not key)

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

    As per your requirement "each value can only be there for once" if you are just interested in keeping unique values in your array, then the array_unique() might be what you are looking for.

    Input:

    $input = array(4, "4", "3", 4, 3, "3");
    $result = array_unique($input);
    var_dump($result);
    

    Result:

    array(2) {
      [0] => int(4)
      [2] => string(1) "3"
    }
    

提交回复
热议问题