计算多维数组中的特定值

时间:2012-07-19 10:01:56

标签: php multidimensional-array counting

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

$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 / conditional'子句(一个SQL)。

任何帮助都会非常感激。

4 个答案:

答案 0 :(得分:11)

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

答案 1 :(得分:10)

$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 />';
    }
}

答案 2 :(得分:5)

你需要的只是一个额外的柜台:

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

答案 3 :(得分:1)

在PHP 5.4+中,您可以使用此简短代码段来计算特定值(甚至无需事先声明$count变量)

array_walk_recursive($fruit, function ($i) use (&$count) {
    $count += (int) ($i === 'green');
});

var_dump($count); // Outputs: int(2)