PHP单词数组计数

时间:2013-01-30 17:30:13

标签: php arrays

我的数组是

  

$ myArray的=阵列( “人”, “天使”,神 “”, “天使”, “魔鬼”, “神”, “神”, “人”, “神”, “天使”); < / p>

如何获得这个词并算上

God : 4
Angel : 3
Human : 2
Devil : 1

3 个答案:

答案 0 :(得分:3)

请参阅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
)

答案 1 :(得分:3)

查看array_count_valuesarsort

<?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/>";
}
?>

答案 2 :(得分:1)

您可以使用array-count-values功能。