我有一个像这样的多维数组。
[
0 => array:3 [
"volume" => 1272.0
"overflow" => 0
"deficit" => 0
]
1 => array:3 [
"volume" => 2352.0
"overflow" => 0
"deficit" => 0
]
2 => array:3 [
"volume" => 3144.0
"overflow" => 0
"deficit" => 0
]
3 => array:3 [
"volume" => 6048.0
"overflow" => 0
"deficit" => 0
]
4 => array:3 [
"volume" => 8952.0
"overflow" => 0
"deficit" => 0
]
]
我有$ count变量,当嵌套数组中溢出和不足都为零时,它会递增。为此,我尝试以下循环。
$keys = array_keys($waterLevels);
$count = 0;
for ($i = 0; $i < count($waterLevels); $i++){
foreach ($waterLevels[$keys[$i]] as $key => $value)
{
if (($key === 'overflow' and $value == 0) and ($key === 'deficit' and $value == 0)){
$count = $count + 1;
}
}
}
如果我运行这个循环,我应该将$ count值设为5.我知道'if'条件有问题。如果我单独尝试'溢出',它就可以了。如果我添加下一个'和'条件,则不会。我在这里错过了什么逻辑?
答案 0 :(得分:1)
只需要这么多: -
$count = 0;
foreach ($waterLevels as $value){
if ($value['overflow'] == 0 && $value['deficit'] == 0){
$count = $count + 1;
}
}
echo $count;
输出: - https://eval.in/780558