【问题标题】:using foreach and while loop together一起使用 foreach 和 while 循环
【发布时间】:2013-06-27 14:58:10
【问题描述】:

我有一个数组 year_values 包含一些值,例如

$year_values = array();
foreach($year_strings as $str) {
  $year_values[] = explode(",", $str);
}

然后我应用查询从数据库中提取一些值

$sql = "SELECT inventory.holdingcost as holdingcost, 
inventory.ordercost as ordercost, inventory.unitprice as unitprice, 
inventory.lead_time as lead_time, items.id as iid 
FROM inventory, items 
WHERE inventory.item_name = items.item_name";
        mysql_error();

        $result = mysql_query($sql);
    foreach($year_values as $vals) {

      while ($row = mysql_fetch_row($result)) {



      $w = $row[2];
      $e = $row[0];
      $r = $row[1];
      $tt = $row[3];
      $eo = sqrt(2 *  $vals[5] * $r / ($e * $w));
      $eoq = round($eo, 1);
      $ro = $vals[5] / 360;
      $rop = round($ro * $tt);
      $op = round($vals[5] / $eoq, 1);
      $cc = round(89 / $op);
      $h = round((($eoq * $e * $w) / 2), 2);
      $o = round((($r * $vals[5]) / $eoq), 2);
      $z = round($h + $o, 2);


       }
    }

当我在上面的 while 循环中使用 foreach 时,它只将 $year_values 的第一个值作为 $vals[5],我想在 while 内部对数组 $year_values 的每个值进行计算。

如何纠正?

重复发生在 value1 中,即 $val[5] 当前:

Value1      Value 2    Value 3
199           202           0.25
199           205           0.25
199           210           0.25
199           230           0.25
1698          202           0.25
1698          205           0.25
1698          210           0.25
1698          230           0.25

相反,我希望值显示为

Value1      Value 2    Value 3
199          202           0.25
1698         205           0.25
15           210           0.25
971          230           0.25

【问题讨论】:

  • 您需要详细说明您要达到的目标。这段代码的意义何在?它根本不使用该计算的结果。
  • 数组 year_values 包含如下值: Array ( [0] => 2013 [1] => 1 [2] => 205 [3] => [4] => 266 [5] = > 199 ) 数组 ( [0] => 2013 [1] => 1 [2] => 1748 [3] => [4] => 299 [5] => 1698 ) 数组 ( [0] => 2013 [ 1] => 1 [2] => 15 [3] => [4] => 170 [5] => 15) 数组 ( [0] => 2013 [1] => 1 [2] => 1000 [ 3] => [4] => 31 [5] => 971) 数组 ([0] => 2013 [1] => 1 [2] => 975 [3] => [4] => 117 [5 ] => 947 ) 我想使用所有第 5 个索引值进行公式计算,例如:$eo = sqrt(2 *$vals[5]*$r /($e*$w));最后显示结果

标签: php


【解决方案1】:

mysql_fetch_row 将在所有行都被提取后返回 false。要重用它,您应该将它们存储在变量 (like the response made by Jari) 中,或者您可以切换两个循环 (see imulsion's answer)

【讨论】:

  • 啊,抱歉,把你和作者搞混了……我现在才看到你的问题,我会编辑。
  • 我尝试了建议的解决方案,但结果仍然不准确,出现重复,
  • 最后我的问题是固定的解决方案是: foreach($year_values as $key => $vals) { $w = $rows[$key][2]; $e = $rows[$key][0]; $r = $rows[$key][1]; $tt = $rows[$key][3]; $eo = sqrt(2 * $vals[5] * $r / ($e * $w)); $eoq = 回合($eo, 1); $ro = $vals[5] / 360; $rop = 回合($ro * $tt); $op = round($vals[5] / $eoq, 1); $cc = 回合(89 / $op); $h = round((($eoq * $e * $w) / 2), 2); $o = round((($r * $vals[5]) / $eoq), 2); }
【解决方案2】:

简单地颠倒陈述:

while ($row = mysql_fetch_row($result)) {
foreach($year_values as $vals) {


      $w = $row[2];
      $e = $row[0];
      $r = $row[1];
      $tt = $row[3];
      $eo = sqrt(2 *  $vals[5] * $r / ($e * $w));
      $eoq = round($eo, 1);
      $ro = $vals[5] / 360;
      $rop = round($ro * $tt);
      $op = round($vals[5] / $eoq, 1);
      $cc = round(89 / $op);
      $h = round((($eoq * $e * $w) / 2), 2);
      $o = round((($r * $vals[5]) / $eoq), 2);
      $z = round($h + $o, 2);


       }
    }

【讨论】:

  • 我试过了,但在这种情况下,查询结果对数组 $year_values 中的每个值重复
【解决方案3】:

我猜你只需要存储所有数据,这样你就可以重复使用它。

$result = mysql_query($sql);
$rows = array() ;
while ($row = mysql_fetch_row($result)){
  $rows[] = $row ;
}

foreach($year_values as $vals) {
  foreach($rows as $row){
   $w = $row[2];
   $e = $row[0];
   $r = $row[1];
   $tt = $row[3];
   $eo = sqrt(2 *  $vals[5] * $r / ($e * $w));
   $eoq = round($eo, 1);
   $ro = $vals[5] / 360;
   $rop = round($ro * $tt);
   $op = round($vals[5] / $eoq, 1);
   $cc = round(89 / $op);
   $h = round((($eoq * $e * $w) / 2), 2);
   $o = round((($r * $vals[5]) / $eoq), 2);
   $z = round($h + $o, 2);
  }
}

【讨论】:

  • $vals[5] 中的每个值现在都在重复
猜你喜欢
  • 2011-08-06
  • 2013-07-06
  • 1970-01-01
  • 1970-01-01
  • 2015-09-23
  • 1970-01-01
  • 2015-07-03
  • 2018-05-20
  • 2013-10-01
相关资源
最近更新 更多