2013-06-19 148 views
0

我下面的话计算数组中重复的数量?

(“hello”, “apple”, “hello”, “hello”, “apple”, “orange”, “cake”) 

Result here should be 5

阵列接触,你能告诉我,如果有在PHP的库函数,我可以用它来算多少重复的单词出现在我的阵列?任何帮助将不胜感激。

+0

谢谢大家的detaied信息。他们都非常有帮助 –

回答

0
$array = array(“hello”, “apple”, “hello”, “hello”, “apple”, “orange”, “cake”); 
$unique_elements = array_unique($array); 
$totalUniqueElements = count($unique_elements); 
echo $totalUniqueElements; 
//Output 5 

Hope this will help you. 
+0

@nickb感谢您改进我的文本格式 –

0

尝试这样

$count1 = count($array); 
$count2 = count(array_unique($array)); 
echo $count1 - $count2; 
+0

对不起,我认为他需要独特的数量....更新我的答案。 – Gautam3164

0

你可以这样做:

$org = count($array); 
$unique = count(array_unique($array)); 
$duplicates = $org - $unique