在getMostFrequentlyOccurringItem()下面创建函数的最佳方法是什么?
//should return "paragraph"
echo getMostFrequentlyOccurringItem(array('line', 'paragraph', 'paragraph'));
//should return "line"
echo getMostFrequentlyOccurringItem(array('wholeNumber', 'line', 'line', 'line'));
//should return null
echo getMostFrequentlyOccurringItem(array('wholeNumber', 'wholeNumber', 'paragraph', 'paragraph'));
//should return "wholeNumber"
echo getMostFrequentlyOccurringItem(array('wholeNumber', '', '', ''));
function getMostFrequentlyOccurringItem($items) {
//...
}
Run Code Online (Sandbox Code Playgroud)
谢谢亚当,这是我完成的解决方案:http: //tanguay.info/web/index.php?pt = codeExamples&id = 396
首先array_count_values(),按照您的喜好按摩输出.
php> =array_count_values(array('wholeNumber', 'line', 'line', 'line'))
array(
"wholeNumber" => 1,
"line" => 3,
)
Run Code Online (Sandbox Code Playgroud)
arsort($counts, SORT_NUMERIC)将array_count_values()首先按频率对输出进行排序.