Return all array elements except for a given key

后端 未结 7 1325
暖寄归人
暖寄归人 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:23

    Although, this question is too old and there are several answer are there for this question, but I am posting a solution that might be useful to someone.

    You may get the all array elements from provided input except the certain keys you've defined to exclude using:

    $result = array_diff_key($input, array_flip(["SomeKey1", "SomeKey2", "SomeKey3"]));
    

    This will exclude the elements from $input array having keys SomeKey1, SomeKey2 and SomeKey3 and return all others into $result variable.

提交回复
热议问题