我在計算基於條件的多維數組中出現的特定值的次數。這是一個示例數組;在多維數組中計算特定值
$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 /條件'子句(一個SQL)的方法。
任何幫助將非常感激。
而不是回聲的名字,做數。 0 + 1 + 1 + 1 + 1 .... – hakre 2012-07-19 10:08:02