本文介绍了PHP - 计算特定的数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何计算值等于常量的数组中元素的数量?例如,
How can I count the number of element inside an array with value equals a constant?example,
$myArray = array("Kyle","Ben","Sue","Phil","Ben","Mary","Sue","Ben");
如何直接知道里面有多少个Ben"?
how can I directly know how many "Ben" is inside?
推荐答案
$array = array("Kyle","Ben","Sue","Phil","Ben","Mary","Sue","Ben");
$counts = array_count_values($array);
echo $counts['Ben'];
这篇关于PHP - 计算特定的数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!