Is there a PHP function to remove any/all key/value pairs that have a certain value from an array?

前端 未结 5 1948
予麋鹿
予麋鹿 2021-01-26 11:27

I think questions like this are the reason why I don\'t like working with PHP. The manual is good, if you can find what you are looking for. After reading through the Array Func

5条回答
  •  余生分开走
    2021-01-26 11:30

    I like the array_diff function, but I have my one scripted one if you dont want to pass down an array:

    function array_unset_value($value, &$array) {
        $key = array_search($value, $array);
        while ($key !== false) {
            unset($array[$key]);
            $key = array_search($value, $array);
        }
    }
    

提交回复
热议问题