[英]Count how often a particular value appears in an array
I know the function count()
of php, but what's the function for counting how often a value appear in an array? 我知道php的函数count()
,但是计算一个值出现在数组中的频率的函数是什么?
Example: 例:
$array = array(
[0] => 'Test',
[1] => 'Tutorial',
[2] => 'Video',
[3] => 'Test',
[4] => 'Test'
);
Now I want to count how often "Test" appears. 现在我想计算“测试”出现的频率。
PHP has a function called array_count_values
for that. PHP有一个名为array_count_values
的函数。
Example: 例:
<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>
Output: 输出:
Array
(
[1] => 2
[hello] => 2
[world] => 1
)
Try the function array_count_values
you can find more information about the function in the documentation here: http://www.php.net/manual/en/function.array-count-values.php 尝试使用函数array_count_values
您可以在此处的文档中找到有关该函数的更多信息: http : array_count_values
Example from that page: 该页面的示例:
<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>
Will produce: 会产生:
Array
(
[1] => 2
[hello] => 2
[world] => 1
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.