-4

我的数组是

$myarray=array("人","天使",神"","天使","恶魔","神","神","人","神","天使");

如何获得单词和数像

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

3 回答 3

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
)
于 2013-01-30T17:32:31.037 回答
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/>";
}
?>
于 2013-01-30T17:40:27.947 回答
1

您可以使用数组计数值函数。

于 2013-01-30T17:32:01.630 回答