How to find unique values in array

后端 未结 4 1563
小鲜肉
小鲜肉 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
    )
    

提交回复
热议问题