PHP 字数数组计数

PHP words array count

提问人:user1866612 提问时间:1/31/2013 最后编辑:Mxykuser1866612 更新时间:2/5/2013 访问量:1202

问:

我的数组是

$myarray=array(“人类”,“天使”,上帝“,”天使“,”魔鬼“,”上帝“,”上帝“,”人类“,”上帝“,”天使“);

如何获得单词并计数

God : 4
Angel : 3
Human : 2
Devil : 1
PHP 数组

评论

0赞 j08691 1/31/2013
你试过什么吗?
4赞 Evan Mulawski 1/31/2013
这是一个“阅读文档”的问题。

答:

1赞 codaddict 1/31/2013 #1

您可以使用 array-count-values 函数。

3赞 Martin 1/31/2013 #2

请参见array_count_values

从文档中:

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

Array
(
  [1] => 2
  [hello] => 2
  [world] => 1
)
3赞 Tom 1/31/2013 #3

看看array_count_values整理

<?php  
$myarray = array("Human","Angel","God","Angel","Devil","God","God","Human","God","Angel");

$result = array_count_values($myarray);
arsort($result);

foreach($result as $word => $count)
{
    echo $word." was found ".$count." time(s)<br/>";
}
?>