array_count_values()是PHP中的内置函数,用于对数组内的所有值进行计数。换句话说,我们可以说array_count_values()函数用于计算数组中所有元素的频率。
用法:
array array_count_values( $array )
参数:该函数接受单个参数$array。此参数是我们需要为其计算其中的值计数的数组。
返回值:此函数返回具有键-值对的关联数组,其中键是作为参数传递的数组元素,而值是这些元素在数组中的出现频率。
注意:如果元素不是字符串或整数,则抛出E_WARNING。
例子:
Input : array = ("Geeks", "for", "Geeks", "Geeks", "Welcome", "for")
Output : 
        Array
        (
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        )
Input : array = (1, 1, 2, 3 , 1 , 2 , 4, 5)
Output :
       Array
       (
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ) 
以下示例程序旨在说明array_count_values()函数在PHP中的工作:
<?php 
  
// PHP code to illustrate the working 
// of array_count_values() function 
function Counting($array){ 
    return(array_count_values($array)); 
} 
  
// Driver Code 
$array = array("Geeks", "for", "Geeks", "Geeks", "Welcome", "for"); 
print_r(Counting($array)); 
  
?>输出:
Array
(
    [Geeks] => 3
    [for] => 2
    [Welcome] => 1
)
参考: http://php.net/manual/en/function.array-count-values.php
相关用法
- p5.js nfc()用法及代码示例
- p5.js nfp()用法及代码示例
- d3.js d3.hcl()用法及代码示例
- p5.js nfs()用法及代码示例
- PHP cos( )用法及代码示例
- PHP sin( )用法及代码示例
- p5.js nf()用法及代码示例
- PHP tan( )用法及代码示例
- PHP pow( )用法及代码示例
- d3.js d3.map.set()用法及代码示例
- d3.js d3.set.has()用法及代码示例
- PHP Ds\Set xor()用法及代码示例
注:本文由纯净天空筛选整理自Chinmoy Lenka大神的英文原创作品 PHP | array_count_values() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
