11

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

$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)的方法。

任何帮助将非常感激。

4

4 回答 4

13

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']);

这种辅助函数通常对多维数组很有用。它也被建议作为 PHP 5.5 的新 PHP 函数

于 2012-07-19T10:22:48.137 回答
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 />';
    }
}
于 2012-07-19T10:04:30.250 回答
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";
}
于 2012-07-19T10:04:31.593 回答
1

使用 PHP 5.4+,您可以使用这个简短的代码片段来计算特定值(甚至不需要$count事先声明变量)

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

var_dump($count); // Outputs: int(2)
于 2020-04-22T02:34:39.567 回答