简体   繁体   English

PHP - 计算特定的数组值

[英]PHP - count specific array values

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");
$array = array("Kyle","Ben","Sue","Phil","Ben","Mary","Sue","Ben");
$counts = array_count_values($array);
echo $counts['Ben'];

You can do this with array_keys<\/em> and count<\/em> .您可以使用array_keys<\/em>和count<\/em>来做到这一点。

$array = array("blue", "red", "green", "blue", "blue");
echo count(array_keys($array, "blue"));

To count a value in a two dimensional array, here is the useful snippet to process and get count of a particular value<\/strong> -要计算二维数组中的值,这里是处理和获取特定值计数<\/strong>的有用片段 -

    <?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
    

Use the array_count_values function.使用array_count_values函数。

$countValues = array_count_values($myArray); $countValues = array_count_values($myArray);

echo $countValues["Ben"];

Use array_count_values()<\/code> function .使用array_count_values()<\/code>函数。 Check this link http:\/\/php.net\/manual\/en\/function.array-count-values.php<\/a>检查此链接http:\/\/php.net\/manual\/en\/function.array-count-values.php<\/a>

"

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";

try the array_count_values() function尝试 array_count_values() 函数

<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>

试试 PHP 函数array_count_values

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)

array_count_values only works for integers and strings. array_count_values仅适用于整数和字符串。 If you happen to want counts for float/numeric values (and you are heedless of small variations in precision or representation), this works:如果您碰巧需要浮点/数值的计数(并且您不注意精度或表示的微小变化),则可以使用:

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 $cnts with the keys as strings.请注意,我将$cnts与键作为字符串返回。 It would be easy to reconvert them, but I'm trying to determine the mode for the values, so I only need to re-convert the first (several) values.重新转换它们很容易,但我正在尝试确定值的模式,所以我只需要重新转换第一个(几个)值。

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%)!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM