php: check if an array has duplicates

前端 未结 15 2428
情深已故
情深已故 2020-11-27 04:07

I\'m sure this is an extremely obvious question, and that there\'s a function that does exactly this, but I can\'t seem to find it. In PHP, I\'d like to know if my array has

15条回答
  •  甜味超标
    2020-11-27 04:45

    I know you are not after array_unique(). However, you will not find a magical obvious function nor will writing one be faster than making use of the native functions.

    I propose:

    function array_has_dupes($array) {
       // streamline per @Felix
       return count($array) !== count(array_unique($array));
    }
    

    Adjust the second parameter of array_unique() to meet your comparison needs.

提交回复
热议问题