[英]How to count number of consecutive null values in array
我有一組客戶,其中有一組嵌套的付款。
"customer_1" => array:4 [▼
0 => "211.79"
1 => "206.20"
2 => "0.00"
3 => "0.00"
4 => "220.90"
]
"customer_2" => array:4 [▼
0 => "0.00"
1 => "0.00"
2 => "0.00"
3 => "0.00"
4 => "220.90"
]
我需要為每個客戶計算連續付款的金額,從 0.00 的數組頂部開始。
所以我需要它返回類似的東西:
"customer_1" => 0
"customer_2" => 4
我已經嘗試了一堆 while 和 foreach 循環,但無法讓它工作:
@php($count = 0)
@foreach($array as $arr)
@if($arr = "0.00")
@php($count = $count + 1)
@else
@continue
@endif
@endforeach
檢查第一個元素,如果它是0.00
,那么只需計算連續的0.00
,或者只是打破循環:
$count = 0;
if ($array[0] == "0.00") {
foreach($array as $item) {
if($item == "0.00") {
$count += 1;
} else {
break;
}
}
}
return $count;
對於刀片:
@php($count = 0)
@if($arr[0] == "0.00")
@foreach($array as $arr)
@if($arr == "0.00")
@php($count += 1)
@else
@break
@endif
@endforeach
@endif
您正在為 if 條件內的變量賦值,您需要在 if 條件內比較“0.00”。
@php($count = 0)
@foreach($array as $arr)
@if($arr == "0.00")
@php($count = $count + 1)
@else
@continue
@endif
@endforeach
$sum = 0;
foreach($items as $item) {
$sum += $item;
}
echo $sum;
試試這個@dexx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.