计算特定值在数组中出现的频率 [英] Count how often a particular value appears in an array

查看:102
本文介绍了计算特定值在数组中出现的频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道php,
的函数 count(),但是计算一个值出现在数组中的频率是多少?



示例:

  $ array = array(
[0] => ;'Test',
[1] =>'教程',
[2] =>'视频',
[3] =>'测试',
[4] =>'Test'
);

现在我想计算Test出现的频率。


< PHP有一个名为

例如:





$ b $ p
$ b

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

输出:

 code> Array 

[1] => 2
[hello] => 2
[world] => 1


I know the function count() of php, but what's the function for counting how often a value appear in an array?

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.

Example:

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

Output:

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

这篇关于计算特定值在数组中出现的频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆