在foreach之后php不会设置 [英] php unset after foreach
问题描述
$ $ p $
foreach($ price as $ key => $ value){
$ number [$ key] = str_pad($ number [$ key],2);
$ type [$ key] = str_pad($ type [$ key],21);
$ lenght [$ key] = str_pad($ lenght [$ key],15);
$ boots [$ key] = str_pad($ boots [$ key],22);
$ size [$ key] = str_pad($ size [$ key],15);
$ pricekipers [$ key] =($ priceski [$ key])*($ person [$ key]);
$ pricebootspers [$ key] =($ priceboots [$ key])*($ person [$ key]);
$ total [$ key] = array_sum(array($ priceskipers [$ key],$ pricebootspers [$ key]));
$ supertotal = 0;
$ id ++;
echo< pre> Data:。$ date [$ key]。 |。 $ number [$ key]。 /。 $ person [$ key]。 |。 $ type [$ key]。 |。 $ lenght [$ key]。 |。 $ boots [$ key]。 |。 $ size [$ key]。 < br>。 $ priceskipers [$ key]。 +。 $ pricebootspers [$ key]。 =。 $ total [$ key]。 (id。$ id。)。< hr>< / pre>;
unset($ value);
foreach($ total为$ key2 => $ value2){
$ supertotal + = $ total [$ key2];
$ discount =(($ supertotal)* 0.2);
$ grandtotal =(($ supertotal) - ($ discount));
}
}
echo< pre>< b> Total。 $ supertotal。 - 20%(在线租赁折扣)。 $折扣。 =。 $ grandtotal。 ,您的净金额。< / b>< / pre>;
首先,你的真正的输入变量,使社区可以更容易地模拟。
即使上面的代码是不正确的方式,但仍然有效。 unset()
调用没有意义。
接下来,稍微重构一下, code> foreach 阻止主循环:#1
foreach($ price ($ key => $ value2){
$ supertotal + = $ total [$ key2];
$ discount =(($ supertotal)* 0.2);
$ grandtotal =(($ supertotal) - ($ discount));
}
接下来,您甚至可以在 foreach
#1循环:
$ superTotal = array_sum($ total);
$ discount = $ superTotal * 0.2;
$ grandTotal = $ superTotal - $ discount;
最后,您的评论是关于什么时候所有用户点击付款按钮然后事情被打破,这意味着那么计算完全取决于点击付款按钮
之后的事件。
I have a kind of store where members of a ski club can rent equipment. The first step is a loop where the member can make repetitive choices, for example for him and his family. Each line of material has its own total with a grand total at the bottom. A problem occurs once the last renting has been done and the member want to finalize his payment. The line of the last rented equipment is doubled and as a result the grand total is false. An idea how to solve this. I think it has something to do with my UNSET instruction. Thanks for helping. Here is the code of my foreach loops:
foreach ($price as $key => $value) {
$number[$key] = str_pad($number[$key], 2);
$type[$key] = str_pad($type[$key], 21);
$lenght[$key] = str_pad($lenght[$key], 15);
$boots[$key] = str_pad($boots[$key], 22);
$size[$key] = str_pad($size[$key], 15);
$priceskipers[$key] = ($priceski[$key]) * ($person[$key]);
$pricebootspers[$key] = ($priceboots[$key]) * ($person[$key]);
$total[$key] = array_sum(array($priceskipers[$key], $pricebootspers[$key]));
$supertotal = 0;
$id++;
echo "<pre>Data : " .$date[$key] . " | " . $number[$key] . " / " . $person[$key] . "| " . $type[$key] . " | " . $lenght[$key] . " | " . $boots[$key] . " | " . $size[$key] . "<br> " . $priceskipers[$key] . " + " . $pricebootspers[$key] . " = " . $total[$key] . " for this reservation (id" . $id . ").<hr></pre>";
unset($value);
foreach ($total as $key2 => $value2) {
$supertotal += $total[$key2];
$discount = (($supertotal) * 0.2);
$grandtotal = (($supertotal) - ($discount));
}
}
echo "<pre><b>Total " . $supertotal . " - 20% (discount for online renting) " . $discount . " = " . $grandtotal . ", your net amount.</b></pre>";
First of all, better you should provide fully each of your real input variables so that community can simulate easier.
Even the code above was not a proper way but it still works. No point for the unset()
call to go here.
Next, a bit refactor, move the last foreach
block out of the main loop: #1
foreach ($price as $key => $value) {
//old logic
}
foreach ($total as $key2 => $value2) {
$supertotal += $total[$key2];
$discount = (($supertotal) * 0.2);
$grandtotal = (($supertotal) - ($discount));
}
Next, you can even remove the whole calculation in foreach
loop of #1 by:
$superTotal = array_sum($total);
$discount = $superTotal * 0.2;
$grandTotal = $superTotal - $discount;
Last, your comment is about when after all user click on pay button then things get broken, it means the calculation then totally depend on how the event after click on pay button
.
这篇关于在foreach之后php不会设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!