PHP 统计数组中每一项出现次数

PHP · 07-24

使用array_count_values函数可以找出数组中相同值出现的次数,array_count_values用法如下:

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

输出

Array
(
    [1] => 2
    [hello] => 2
    [world] => 1
)
Theme Jasmine by Kent Liao