【问题标题】:Counting occurrence of specific value in an Array with PHP [duplicate]用PHP计算数组中特定值的出现[重复]
【发布时间】:2011-05-10 04:36:05
【问题描述】:

我正在尝试找到一个原生 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

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

【问题讨论】:

    标签: php arrays


    【解决方案1】:
    function array_count_values_of($value, $array) {
        $counts = array_count_values($array);
        return $counts[$value];
    }
    

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

    或者:

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

    或者:

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

    或者:

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

    【讨论】:

    • 就我个人而言,我会选择array_reduce 版本,因为我很喜欢折叠。 :o)
    • count(array_keys($array, 6, $strict = false)) 中可以提供$strict,如果提供为true 将使用严格比较(=== 运算符而不是==
    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

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

      $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 )
      

      details

      【讨论】:

        【解决方案4】:

        从 PHP 5.4.0 开始,您可以对由 array_count_values() 生成的数组的索引 [6] 使用函数数组解引用:

        $instances = array_count_values($array)[6];
        

        所以检查并分配0如果没有找到:

        $instances = array_count_values($array)[6] ?? 0;
        

        【讨论】:

        • 如果有 0 个值,这将引发错误。共 6 个。
        • @Andrei 添加了一个选项。
        【解决方案5】:

        假设我们有以下数组:

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

        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) 您接下来需要做的就是在每次要计算任何数组中的任何特定值时应用该函数。例如:

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

        【讨论】:

        • 如果可能,请解释您的代码是如何工作的,或者它是如何解决问题的。
        • @Compass 我解释了函数并添加了一个示例。希望有帮助!
        【解决方案6】:

        这正是你要找的东西

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

        【讨论】:

          猜你喜欢
          • 2019-09-29
          • 1970-01-01
          • 2016-03-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多