2010-03-13 69 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 
) 
相关问题