![](/img/trans.png)
[英]get the most listed items in a array in php without removing duplicates (5)
[英]PHP get the item in an array that has the most duplicates
我有一個字符串數組,我正在尋找一種方法來找到數組中最常見的字符串。
$stuff = array('orange','banana', 'apples','orange');
我想看到橙色。
$c = array_count_values($stuff);
$val = array_search(max($c), $c);
使用array_count_values
並獲取項目的鍵:
<?php
$stuff = array('orange','banana', 'apples','orange', 'xxxxxxx');
$result = array_count_values($stuff);
asort($result);
end($result);
$answer = key($result);
echo $answer;
?>
輸出:
orange
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.