使用 PHP 计算数组中特定值的出现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5945199/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:59:06  来源:igfitidea点击:

Counting occurrence of specific value in an Array with PHP

phparrays

提问by Javit

I am trying to find a native PHP function that will allow me to count the number of occurrences of a particular value in an array. I am familiar with the array_count_values() function, but that returns the count of all values in an array. Is there a function that allows you to pass the value and just return the instance count for that particular value? For example:

我试图找到一个本机 PHP 函数,它允许我计算数组中特定值的出现次数。我熟悉 array_count_values() 函数,但它返回数组中所有值的计数。是否有一个函数允许您传递值并只返回该特定值的实例计数?例如:

$array = array(1, 2, 3, 3, 3, 4, 4, 5, 6, 6, 6, 6, 7);

$instances = some_native_function(6, $array);  //$instances will be equal to 4

I know how to create my own function, but why re-invent the wheel?

我知道如何创建自己的函数,但为什么要重新发明轮子呢?

回答by deceze

function array_count_values_of($value, $array) {
    $counts = array_count_values($array);
    return $counts[$value];
}

Not native, but come on, it's simple enough. ;-)

不是native,但是来吧,它很简单。;-)

Alternatively:

或者:

echo count(array_filter($array, function ($n) { return $n == 6; }));

Or:

或者:

echo array_reduce($array, function ($v, $n) { return $v + ($n == 6); }, 0);

Or:

或者:

echo count(array_keys($array, 6));

回答by Muhammad Zeeshan

This solution may be near to your requirement

此解决方案可能接近您的要求

$array = array(1, 2, 3, 3, 3, 4, 4, 5, 6, 6, 6, 6, 7);
print_r(array_count_values($array));

Result:

Array
( [1] => 1 ,[2] => 1 , [3] => 3, [4] => 2,[5] =>1, [6] => 4, [7] => 1 )

for details.

对于细节

回答by AbraCadaver

As of PHP 5.4.0 you can use function array dereferencing for the index [6]of the array resulting from array_count_values():

从 PHP 5.4.0 开始,您可以使用函数数组解引用作为[6]由以下结果产生的数组索引array_count_values()

$instances = array_count_values($array)[6]; //$instances will be equal to 4

回答by Kareem

Assume we have the following array:

假设我们有以下数组:

     $stockonhand = array( "large green", "small blue", "xlarge brown", "large green", "medieum yellow", "xlarge brown",  "large green");

1) Copy and paste this function once on top your page.

1) 将此功能复制并粘贴到页面顶部。

    function arraycount($array, $value){
    $counter = 0;
    foreach($array as $thisvalue) /*go through every value in the array*/
     {
           if($thisvalue === $value){ /*if this one value of the array is equal to the value we are checking*/
           $counter++; /*increase the count by 1*/
           }
     }
     return $counter;
     }

2) All what you need to do next is to apply the function every time you want to count any particular value in any array. For example:

2)接下来您需要做的就是在每次要计算任何数组中的任何特定值时应用该函数。例如:

     echo arraycount($stockonhand, "large green"); /*will return 3*/

     echo arraycount($stockonhand, "xlarge brown"); /*will return 2*/

回答by sachin tendulkar

Say I have an array like this:

假设我有一个这样的数组:

$array = array('', '', 'other', '', 'other');

$counter = 0;
foreach($array as $value)
{
  if($value === '')
    $counter++;
}
echo $counter;

This gives the number of times ' ' is repeating in the array

这给出了 ' ' 在数组中重复的次数

回答by Muhammad Bilal

This is exactly what you looking for

这正是你要找的

<?php
 $MainString = 'Yellow Green Orange Blue Yellow Black White Purple';
 $FinderString = 'Yellow Blue White';
 $mainArray = explode(" ", $MainString);
 $findingArray = explode(" ", $FinderString);
 $count = 0;
 $eachtotal = array_count_values($mainArray);
 foreach($findingArray as $find){

    $count += $eachtotal[$find];
 }
 echo $count;


?>