簡體   English   中英

PHP獲取具有最多重復項的數組中的項

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM