开发者

Count how often a particular value appears in an array

开发者 https://www.devze.com 2022-12-23 06:02 出处:网络
I know the function co开发者_如何学JAVAunt() of php, but what\'s the function for counting how often a value appear in an array?

I know the function co开发者_如何学JAVAunt() 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
)


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

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
)
0

精彩评论

暂无评论...
验证码 换一张
取 消