PHP的话阵列数 [英] PHP words array count

查看:139
本文介绍了PHP的话阵列数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的阵列


  

$ myArray的=阵列(人,天使,神,天使,魔鬼,上帝,上帝,人,上帝,天使);


如何让文字和计数像

 神:4
天使:3
人类:2
恶魔:1


解决方案

看看的 array_count_values​​ arsort

 < PHP
$ myArray的=阵列(人,天使,上帝,天使,魔鬼,上帝,上帝,人,上帝,天使);$结果= array_count_values​​($ myarray的);
arsort($结果);的foreach($结果为$字=> $计数)
{
    回声$词被发现$计数。时间(s)< BR />中;
}
?>

My array is

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

How to get the word and count like

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

解决方案

Take a look at array_count_values and arsort.

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

这篇关于PHP的话阵列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆