PHP get the item in an array that has the most duplicates

前端 未结 2 1568
难免孤独
难免孤独 2021-02-07 02:08

I have an array of strings and I am looking for a way to find the most common string in the array.

$stuff = array(\'orange\',\'banana\', \'apples\',\'orange\');
         


        
相关标签:
2条回答
  • 2021-02-07 02:23

    Use array_count_values and get the key of the item:

    <?php
    $stuff = array('orange','banana', 'apples','orange', 'xxxxxxx');
    
    $result = array_count_values($stuff);
    asort($result);
    end($result);
    $answer = key($result);
    
    echo $answer;
    ?>
    

    Output:

    orange
    
    0 讨论(0)
  • 2021-02-07 02:42
    $c = array_count_values($stuff); 
    $val = array_search(max($c), $c);
    
    0 讨论(0)
提交回复
热议问题