How to remove duplicate values from an array in PHP

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

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

相关标签:
24条回答
  • 2020-11-22 04:08

    I have done this without using any function.

    $arr = array("1", "2", "3", "4", "5", "4", "2", "1");
    
    $len = count($arr);
    for ($i = 0; $i < $len; $i++) {
      $temp = $arr[$i];
      $j = $i;
      for ($k = 0; $k < $len; $k++) {
        if ($k != $j) {
          if ($temp == $arr[$k]) {
            echo $temp."<br>";
            $arr[$k]=" ";
          }
        }
      }
    }
    
    for ($i = 0; $i < $len; $i++) {
      echo $arr[$i] . " <br><br>";
    }
    
    0 讨论(0)
  • 2020-11-22 04:14
    //Find duplicates 
    
    $arr = array( 
        'unique', 
        'duplicate', 
        'distinct', 
        'justone', 
        'three3', 
        'duplicate', 
        'three3', 
        'three3', 
        'onlyone' 
    );
    
    $unique = array_unique($arr); 
    $dupes = array_diff_key( $arr, $unique ); 
        // array( 5=>'duplicate', 6=>'three3' 7=>'three3' )
    
    // count duplicates
    
    array_count_values($dupes); // array( 'duplicate'=>1, 'three3'=>2 )
    
    0 讨论(0)
  • 2020-11-22 04:14
    $a = array(1, 2, 3, 4); 
    $b = array(1, 6, 5, 2, 9); 
    $c = array_merge($a, $b);
    $unique = array_keys(array_flip($c));
    print_r($unique);
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-11-22 04:17

    The only thing which worked for me is:

    $array = array_unique($array, SORT_REGULAR);
    

    Edit : SORT_REGULAR keeps the same order of the original array.

    0 讨论(0)
  • 2020-11-22 04:17
    <?php
    $arr1 = [1,1,2,3,4,5,6,3,1,3,5,3,20];    
    print_r(arr_unique($arr1));
    
    
    function arr_unique($arr) {
      sort($arr);
      $curr = $arr[0];
      $uni_arr[] = $arr[0];
      for($i=0; $i<count($arr);$i++){
          if($curr != $arr[$i]) {
            $uni_arr[] = $arr[$i];
            $curr = $arr[$i];
          }
      }
      return $uni_arr;
    }
    
    0 讨论(0)
提交回复
热议问题