How to find unique values in array

后端 未结 4 1526
小鲜肉
小鲜肉 2021-01-24 06:41

Here i want to find unique values,so i am writing code like this but i can\'t get answer,for me don\'t want to array format.i want only string

<         


        
相关标签:
4条回答
  • 2021-01-24 07:05

    To get unique values from array use array_unique():

    $array = array(1,2,1,5,10,5,10,7,9,1) ;
    array_unique($array);
    print_r(array_unique($array));
    

    Output:

    Array
    (
        [0] => 1
        [1] => 2
        [3] => 5
        [4] => 10
        [7] => 7
        [8] => 9
    )
    

    And to get duplicated values in array use array_count_values():

    $array = array(1,2,1,5,10,5,10,7,9,1) ;
    print_r(array_count_values($array));
    

    Output:

    Array
    (
        [1] => 3 // 1 is duplicated value as it occurrence is 3 times
        [2] => 1
        [5] => 2 // 5 is duplicated value as it occurrence is 3 times
        [10] => 2 // 10 is duplicated value as it occurrence is 3 times
        [7] => 1
        [9] => 1
    )
    
    0 讨论(0)
  • 2021-01-24 07:18
    <?php
      $arr1 = [1,1,2,3,4,5,6,3,1,3,5,3,20];    
      print_r(arr_unique($arr1)); // will print unique values
      echo "<br>";
      arr_count_val($arr1); // will print array with duplicate values
    
      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;
      }
    
      function arr_count_val($arr) {
        $uni_array = arr_unique($arr1);
        $count = 0;
    
        foreach($uni_array as $n1) {
          foreach($arr as $n1) {
            if($n1 == $n2) {
              $count++;
            }
          }
        echo "$n1"." => "."$count"."<br>";
        $count=0;
        }
      }
    ?>
    
    0 讨论(0)
  • 2021-01-24 07:18

    I am baffled by your selection as the accepted answer -- I can't imagine how it can possibly satisfy your requirements.

    array_count_values() has been available since PHP4, so that is the hero to call upon here.

    Code: (Demo)

    $array = array("kani","yuvi","raja","kani","mahi","yuvi") ;
    
    $counts = array_count_values($array);  // since php4
    foreach ($counts as $value => $count) {
        if ($count == 1) {
            $uniques[] = $value;
        } else {
            $duplicates[] = $value;
        }
    }
    
    echo "unique values: " , implode(", ", $uniques) , "\n";
    echo "duplicate values: " , implode(", ", $duplicates);
    

    Output:

    unique values: raja, mahi
    duplicate values: kani, yuvi
    
    0 讨论(0)
  • 2021-01-24 07:21

    You can use array_unique() in single line like below:-

    <?php
    
    $unique_array = array_unique($array); // get unique value from initial array
    
    echo "<pre/>";print_r($unique_array); // print unique values array
    ?>
    

    Output:- https://eval.in/601260

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

    If you want in string format:-

    echo implode(',',$final_array);
    

    Output:-https://eval.in/601261

    The way you want is here:-

    https://eval.in/601263

    OR

    https://eval.in/601279

    0 讨论(0)
提交回复
热议问题