PHP 计算数组中的单词数

PHP Count words in array

好的,所以在我的例子中,我有一个类似这样的数组(但更大)

$array = [
    0 => "TOD",
    1 => "TOD",
    2 => "MAX",
    3 => "JEFF",
    4 => "TOD",
    5 => "MAX",
    6 => "JEFF",
    7 => "MAX",
    8 => "MAX"
];

现在我的问题是,有没有办法计算TODJEFFMAX在数组$array中出现的次数,然后存储它们分成单独的变量,因此例如期望的结果是有 3 个单独的变量,这些变量作为值(基于上面显示的示例代码)

$todAmount = 3;
$jeffAmount = 2;
$maxAmount = 4;

我已经做了很多研究,但还没有真正找到一个好的方法来做到这一点:/
感谢阅读!

那应该对你有帮助...

http://php.net/manual/en/function.array-count-values.php

尝试

print_r(array_count_values($array));

array_count_values() 可能就是您所需要的。

<?php

$array = [
    0 => "TOD",
    1 => "TOD",
    2 => "MAX",
    3 => "JEFF",
    4 => "TOD",
    5 => "MAX",
    6 => "JEF",
    7 => "MAX",
    8 => "MAX"
];
    print_r(array_count_values($array));
?>