PHP - 计算特定的数组值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11646054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - count specific array values
提问by Ivo San
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");
how can I directly know how many "Ben" is inside?
我怎么能直接知道里面有多少“本”?
回答by Rajat Singhal
$array = array("Kyle","Ben","Sue","Phil","Ben","Mary","Sue","Ben");
$counts = array_count_values($array);
echo $counts['Ben'];
回答by Oliver A.
You can do this with array_keysand count.
你可以用array_keys和count来做到这一点。
$array = array("blue", "red", "green", "blue", "blue");
echo count(array_keys($array, "blue"));
Output:
输出:
3
回答by Rikesh
Use the array_count_valuesfunction.
使用array_count_values函数。
$countValues = array_count_values($myArray);
$countValues = array_count_values($myArray);
echo $countValues["Ben"];
echo $countValues["Ben"];
回答by Vishal Kumar Sahu
In my case of two dimensional array, from PHP Official Page comments, I found the useful snippet for a two dimensional array-
在我的二维数组的情况下,从 PHP 官方页面的评论中,我找到了二维数组的有用片段-
<?php
$list = [
['id' => 1, 'userId' => 5],
['id' => 2, 'userId' => 5],
['id' => 3, 'userId' => 6],
];
$userId = 5;
echo array_count_values(array_column($list, 'userId'))[$userId]; // outputs: 2
?>
回答by swapnesh
Use array_count_values()function . Check this link http://php.net/manual/en/function.array-count-values.php
使用array_count_values()功能。检查此链接http://php.net/manual/en/function.array-count-values.php
回答by Leo
define( 'SEARCH_STRING', 'Ben' );
$myArray = array("Kyle","Ben","Sue","Phil","Ben","Mary","Sue","Ben");
$count = count(array_filter($myArray,function($value){return SEARCH_STRING === $value;}));
echo $count, "\n";
Output:
输出:
3
回答by Doc
try the array_count_values() function
尝试 array_count_values() 函数
<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>
output:
输出:
Array
(
[1] => 2
[hello] => 2
[world] => 1
)
回答by nairbv
Try the PHP function array_count_values.
试试 PHP 函数array_count_values。
回答by frosty
If you want to count ALL the same occurences inside the array, here's a function to count them all, and return the results as a multi-dimensional array:
如果你想计算数组中所有相同的出现次数,这里有一个函数来计算它们,并将结果作为多维数组返回:
function countSame($array) {
$count = count($array);
$storeArray = array();
while ($count > 0) {
$count--;
if ($array[$count]) {
$a = $array[$count];
$counts = array_count_values($array);
$counts = $counts[$a];
$tempArray = array($a, $counts);
array_push($storeArray, $tempArray);
$keys = array_keys($array, $a);
foreach ($keys as $k) {
unset($array[$k]);
} //end of foreach ($keys as $k)
} //end of if ($array[$count])
} //end of while ($count > 0)
return $storeArray;
} //end of function countSame($array)
回答by dhc
array_count_valuesonly works for integers and strings. If you happen to want counts for float/numeric values (and you are heedless of small variations in precision or representation), this works:
array_count_values仅适用于整数和字符串。如果您碰巧想要对浮点/数字值进行计数(并且您不注意精度或表示法的微小变化),则可以这样做:
function arrayCountValues($arr) {
$vals = [];
foreach ($arr as $val) { array_push($vals,strval($val)); }
$cnts = array_count_values($vals);
arsort($cnts);
return $cnts;
}
Note that I return $cntswith the keys as strings. It would be easy to reconvert them, but I'm trying to determine the modefor the values, so I only need to re-convert the first (several) values.
请注意,我$cnts将键作为字符串返回。重新转换它们很容易,但我正在尝试确定这些值的模式,所以我只需要重新转换第一个(几个)值。
I tested a version which looped, creating an array of counts rather than using array_count_values, and this turned out to be more efficient (by maybe 8-10%)!
我测试了一个循环版本,创建了一个计数数组而不是使用array_count_values,结果证明效率更高(可能提高了 8-10%)!

