How to remove all instances of duplicated values from an array

前端 未结 9 1090
心在旅途
心在旅途 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:25

    PHP.net http://php.net/manual/en/function.array-unique.php

    array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )

    Takes an input array and returns a new array without duplicate values.


    New solution:

    
    function remove_dupes(array $array){
        $ret_array = array();
        foreach($array as $key => $val){
            if(count(array_keys($val) > 1){
                continue;
            } else { 
                $ret_array[$key] = $val; 
            }
    }
    

提交回复
热议问题