How to remove all instances of duplicated values from an array

前端 未结 9 1156
心在旅途
心在旅途 2021-01-02 19:48

I know there is array_unique function, but I want to remove duplicates. Is there a built-in function or do I have to roll my own.

Example input:

9条回答
  •  渐次进展
    2021-01-02 20:12

    Only partially relevant to this specific question - but I created this function from Gumbo's answer for multi dimensional arrays:

    function get_default($array)
    {
        $default = array_column($array, 'default', 'id');
        $array = array_diff($default, array_diff_assoc($default, array_unique($default)));
    
        return key($array);
    }
    

    In this example, I had cached statuses and each one other than the default was 0 (the default was 1). I index the default array from the IDs, and then turn it into a string. So to be clear - the returned result of this is the ID of the default status providing it's in the same part of the multi dimensional array and not the key of it

提交回复
热议问题