My name is Edward Tanguay. I'm an American software and web developer living and working in Berlin, Germany.
PHP CODE EXAMPLE created on Friday, October 29, 2010 permalink
A PHP function which returns the most frequently occuring item
This function shows how you can get the most frequently occurring item out of an array using array_count_values() and arsort().
<?php
//should return "paragraph"
echo getMostFrequentlyOccurringItem(array('line', 'paragraph', 'paragraph')) . '<hr/>';

//should return "line"
echo getMostFrequentlyOccurringItem(array('wholeNumber', 'date', 'date', 'line', 'line', 'line')) . '<hr/>';

//should return null
echo getMostFrequentlyOccurringItem(array('wholeNumber', 'wholeNumber', 'paragraph', 'paragraph')) . '<hr/>';

//should return "wholeNumber"
echo getMostFrequentlyOccurringItem(array('wholeNumber', '', '', '')) . '<hr/>';

function getMostFrequentlyOccurringItem($items) {
    
    //catch invalid entry
    if($items == null) {
        return null;
    }
    if(count($items) == 0) {
        return null;
    }
    
    //sort
    $groups = array_count_values($items);
    arsort($groups);
    
    //if there was a tie, then return null
    $groupAmounts = array_values($groups);
    if($groupAmounts[0] == $groupAmounts[1]) {
        return null;
    }
    
    //get most frequent
    $mostFrequentGroup = '';
    foreach($groups as $group => $numberOfTimesOccurrred) {
        if(trim($group) != '') {
            $mostFrequentGroup = $group;
            break;
        }
    }
    return $mostFrequentGroup;
}

?>
need markup?