How to remove duplicate values from an array in PHP

后端 未结 24 1465
故里飘歌
故里飘歌 2020-11-22 03:40

How can I remove duplicate values from an array in PHP?

24条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 04:16

    It can be done through function I made three function duplicate returns the values which are duplicate in array.

    Second function single return only those values which are single mean not repeated in array and third and full function return all values but not duplicated if any value is duplicated it convert it to single;

    function duplicate($arr) {
        $duplicate;
        $count = array_count_values($arr);
        foreach($arr as $key => $value) {
            if ($count[$value] > 1) {
                $duplicate[$value] = $value;
            }
        }
        return $duplicate;
    }
    function single($arr) {
        $single;
        $count = array_count_values($arr);
        foreach($arr as $key => $value) {
            if ($count[$value] == 1) {
                $single[$value] = $value;
            }
        }
        return $single;
    }
    function full($arr, $arry) {
        $full = $arr + $arry;
        sort($full);
        return $full;
    }
    

提交回复
热议问题