在foreach之后php不会设置 [英] php unset after foreach

查看:146
本文介绍了在foreach之后php不会设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一家商店,一家滑雪俱乐部的成员可以租用设备。第一步是成员可以做出重复选择的循环,例如他和他的家人。每一行的材料都有自己的总数,总数在底部。一旦最后一次租用完成并且成员想要完成他的付款,就会出现问题。最后租用的设备的线路增加了一倍,因此总计是错误的。一个想法如何解决这个问题。我认为这与我的UNSET指令有关。感谢您的帮助。这里是我的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆