How to find unique values in array

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

提交回复
热议问题