Return all array elements except for a given key

后端 未结 7 1344
暖寄归人
暖寄归人 2021-02-02 09:06

Simple one, I was just wondering if there is a clean and eloquent way of returning all values from an associative array that do not match a given key(s)?

$array          


        
7条回答
  •  广开言路
    2021-02-02 09:25

    You could just unset the value:

    $alphaAndGamma = $array;
    unset($alphaAndGamma['alpha']);
    

    Edit: Made it clearer. You can copy an array by assigning it to another variable.

    or in a function:

    function arrayExclude($array, Array $excludeKeys){
        foreach($excludeKeys as $key){
            unset($array[$key]);
        }
        return $array;
    }
    

提交回复
热议问题