如何计算重复出现次数

时间:2015-04-09 03:19:37

标签: php

这是我的关联数组,我试图计算这里的重复次数或每个重复项(r-17-VM)(v-2-VM)忽略"主机上的控制域&#34 ;

Array
(
    [0] => Array
        (
            [0] => r-17-VM
            [1] => r-17-VM
            [2] => r-17-VM
            [3] => r-17-VM
            [4] => r-17-VM
            [5] => r-17-VM
            [6] => r-17-VM
            [7] => r-17-VM
            [8] => r-17-VM
            [9] => r-17-VM
            [10] => r-17-VM
            [11] => r-17-VM
            [12] => r-17-VM
            [13] => v-2-VM
            [14] => v-2-VM
            [15] => v-2-VM
            [16] => v-2-VM
            [17] => v-2-VM
            [18] => v-2-VM
            [19] => v-2-VM
            [20] => v-2-VM
            [21] => v-2-VM
            [22] => v-2-VM
            [23] => v-2-VM
            [24] => v-2-VM
            [25] => v-2-VM
            [26] => Control domain on host
            [27] => Control domain on host
            [28] => Control domain on host
            [29] => Control domain on host
            [30] => Control domain on host
            [31] => Control domain on host
        )

)

谢谢

1 个答案:

答案 0 :(得分:2)

使用array_count_values。它返回每个值重复次数的数组:

$frequency = array_count_values($array);
var_dump($frequency);

对于你的阵列,这输出:

array(3) {
  ["r-17-VM"]=>
  int(13)
  ["v-2-VM"]=>
  int(13)
  ["Control domain on host"]=>
  int(6)
}