2010-03-13 72 views
5

我知道函數count() php, 但是計算數值在數組中出現頻率的函數有什麼功能?計算一個特定值在一個數組中出現的頻率

例子:

$array = array(
    [0] => 'Test', 
    [1] => 'Tutorial', 
    [2] => 'Video', 
    [3] => 'Test', 
    [4] => 'Test' 
); 

現在我要怎麼算經常出現 「測試」。

回答

12

PHP有一個叫做array_count_values的函數。

例子:

<?php 
$array = array(1, "hello", 1, "world", "hello"); 
print_r(array_count_values($array)); 
?> 

輸出:

Array 
(
    [1] => 2 
    [hello] => 2 
    [world] => 1 
) 
2

嘗試的功能array_count_values你可以找到有關的文檔中的功能在這裏的更多信息:http://www.php.net/manual/en/function.array-count-values.php

實例從該頁面:

<?php 
$array = array(1, "hello", 1, "world", "hello"); 
print_r(array_count_values($array)); 
?> 

將生產:

Array 
(
    [1] => 2 
    [hello] => 2 
    [world] => 1 
) 
相關問題