开发者

PHP: count how many times a number appears in an array?

开发者 https://www.devze.com 2023-01-13 03:39 出处:网络
I\'m running a loop basically that will make an array that contains a million numbers between 1 and 10, how do I开发者_开发知识库 iterate through it and count how many of each there are?

I'm running a loop basically that will make an array that contains a million numbers between 1 and 10, how do I开发者_开发知识库 iterate through it and count how many of each there are?

Like:

1 - 201491 times  
2 - 23091 times


There's a native PHP function for that:

$count = array_count_values($array);
print_r($count);

will output:

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

精彩评论

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