Return all array elements except for a given key

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

    Use array_diff_key():

    $array = array('alpha' => 'apple', 'beta' => 'banana', 'gamma' => 'guava');
    
    $alphaAndGamma = array_diff_key($array, array('alpha'=>0));
    $onlyBeta      = array_diff_key($array, array('alpha'=>0, 'gamma'=>0));
    

    EDIT: I added =>0s.

提交回复
热议问题