How to remove all instances of duplicated values from an array

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

    You can use

    $singleOccurences = array_keys(
        array_filter(
            array_count_values(
                array('banana', 'mango', 'banana', 'mango', 'apple' )
            ),
            function($val) {
                return $val === 1;
            }
        )
    )
    

    See

    • array_count_values — Counts all the values of an array
    • array_filter — Filters elements of an array using a callback function
    • array_keys — Return all the keys or a subset of the keys of an array
    • callbacks

提交回复
热议问题