我有以下foreach循环,我怎样才能再次重复相同的循环索引?
$n = 0;
$currentTotal = 0;
$currentSettlement = $coupons[0]->settlement_id;
foreach ($coupons as $coupon){
if($currentSettlement == $coupon->settlement_id){
$currentTotal += $coupon->amount;
}else{
$settlements[$n]['total'] = $currentTotal;
$currentTotal = 0;
$currentSettlement = $coupon->settlement_id;
$n++;
// after this I want to reiterate current loop from the beginning
}
}
答案 0 :(得分:0)
如果你想要必要的foreach循环,你可以使用你修复它只运行两次的函数:
$n = 0;
$currentTotal = 0;
$currentSettlement = $coupons[0]->settlement_id;
for2loop(1,$coupons);
function for2loop($loopagain,$coupons)
{
foreach ($coupons as $coupon) {
if ($currentSettlement == $coupon->settlement_id) {
$currentTotal += $coupon->amount;
} else {
$settlements[$n]['total'] = $currentTotal;
$currentTotal = 0;
$currentSettlement = $coupon->settlement_id;
$n++;
if($loopagain == 1)
for2loop(2,$coupons); // after this I want to reiterate current loop from the beginning
}
}
return $currentSettlement
}
但我认为yu必须更喜欢while循环或更好地循环