【问题标题】:Count specific values in multidimensional array计算多维数组中的特定值
【发布时间】:2012-07-18 11:50:33
【问题描述】:

我正在尝试根据条件计算某个值出现在我的多维数组中的次数。这是一个示例数组;

$fruit = array (
                 "oranges" => array(
                                    "name"    => "Orange",
                                    "color"   => "orange",
                                    "taste"   => "sweet",
                                    "healthy" => "yes"
                              ),
                 "apples" => array(
                                    "name"    => "Apple",
                                    "color"   => "green",
                                    "taste"   => "sweet",
                                    "healthy" => "yes"
                              ),
                 "bananas" => array(
                                    "name"    => "Banana",
                                    "color"   => "yellow",
                                    "taste"   => "sweet",
                                    "healthy" => "yes"
                              ),
                 "grapes" => array(
                                    "name"    => "Grape",
                                    "color"   => "green",
                                    "taste"   => "sweet",
                                    "healthy" => "yes"
                              )
            );

如果我想展示所有绿色水果,我可以执行以下操作(让我知道这是否是最好的方法);

for ($row = 0; $row < 3; $row++) {

    if($fruit[$row]["color"]=="green") {

         echo $fruit[$row]["name"] . '<br />';

    }

}

这将输出;

Apple
Grape

太好了,我可以看到它们有 2 个值,但是我如何才能真正让 PHP 计算颜色为绿色的水果的数量并将其放入变量中,以便我在脚本中进一步使用它来工作出去?例如。我想做类似的事情;

if($number_of_green_fruit > 1) { echo "You have more than 1 piece of green fruit"; }

我看过 count();但我看不到任何添加“WHERE/条件”子句(a la SQL)的方法。

任何帮助将不胜感激。

【问题讨论】:

  • 与其重复名字,不如数数。 0 + 1 + 1 + 1 + 1 ....

标签: php multidimensional-array counting


【解决方案1】:

PHP 不支持SQL where 之类的东西,尤其是不支持数组数组。但是您可以在迭代数据时自己计算:

$count = array();
foreach($fruit as $one)
{
    @$count[$one['color']]++;
}

printf("You have %d green fruit(s).\n", $count['green']);

另一种方法是给自己写一些小辅助函数:

/**
 * array_column
 *
 * @param array $array rows - multidimensional
 * @param int|string $key column
 * @return array;
 */
function array_column($array, $key) {
    $column = array();
    foreach($array as $origKey => $value) {
        if (isset($value[$key])) {
            $column[$origKey] = $value[$key];
        }            
    }
    return $column;
}

然后你可以得到所有颜色:

$colors = array_column($fruit, 'color');

然后计数值:

$count = array_count_values($colors);
printf("You have %d green fruit(s).\n", $count['green']);

这种辅助函数通常对多维数组很有用。也是suggested as a new PHP function for PHP 5.5

【讨论】:

    【解决方案2】:
    $number_of_green_fruit = 0;
    for ($row = 0; $row < 3; $row++) {
        if($fruit[$row]["color"]=="green") {
             $number_of_green_fruit++;
             echo $fruit[$row]["name"] . '<br />';
        }
    }
    

    【讨论】:

      【解决方案3】:

      您只需要一个额外的计数器:

      for ($row = $number_of_green_fruit = 0; $row < 3; $row++) {
          if($fruit[$row]["color"]=="green") {
               echo $fruit[$row]["name"] . '<br />';
               $number_of_green_fruit++;
          }
      }
      
      if($number_of_green_fruit > 1) {
          echo "You have more than 1 piece of green fruit";
      }
      

      【讨论】:

        【解决方案4】:

        使用 PHP 5.4+,您可以使用这个简短的 sn-p 来计算特定值(甚至不需要之前声明 $count 变量)

        array_walk_recursive($fruit, function ($i) use (&$count) {
            $count += (int) ($i === 'green');
        });
        
        var_dump($count); // Outputs: int(2)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-01-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-10-13
          • 1970-01-01
          • 2012-06-26
          • 1970-01-01
          相关资源
          最近更新 更多