如何每次使用不同的变量在 PHP 中重新迭代计算


How to re-iterate calculation in PHP with different variables each time?

我已经编写了一个代码来使用我的公式进行计算,但是我每次都很难让代码使用具有不同变量的公式重新计算。相反,它使用相同的变量。我希望每次重新运行时都使用公式中随机选择的变量之一。请参阅下面的代码。注意我是初学者。

' PIIP 计算

  $PIIPCount = 3 ;
  $Area = array(1000, 1500, 2000, 2500, 3000) ;
  $Height = array(100, 150, 200, 250, 250) ;
  $Poro = array(0.05, 0.10, 0.15, 0.20) ;
  $SatCon = array(0.02, 0.04, 0.06, 0.08) ;
  $OilVolFac = array(1.05, 1.1, 1.15, 1.2) ;
  //$AreaCount = count($Area);
  $AreaCount = $PIIPCount ;
  for ($i = 0; $i < $AreaCount ; $i++ ) {
    $RandA = $Area[array_rand($Area)];
    print "<li> $RandA <li>";
   }
   //$HeightCount = count($Height);
   $HeightCount = $PIIPCount ;
   for ($j = 0; $j < $HeightCount ; $j++ ) {
    $RandH = $Height[array_rand($Height)];
    print "<li> $RandH <li>";
   }
   // $PoroCount = count($Poro);
  $PoroCount = $PIIPCount ;
  for ($k = 0; $k < $PoroCount ; $k++ ) {
    $RandP = $Poro[array_rand($Poro)];
    print "<li> $RandP <li>";
   }
   // $SatConCount = count($SatCon);
   $SatConCount = $PIIPCount ;
    for ($l = 0; $l < $SatConCount ; $l++ ) {
     $RandS = $SatCon[array_rand($SatCon)];
     print "<li> $RandS <li>";
    }
     // $OilVolFacCount = count($OilVolFac);
     $OilVolFacCount = $PIIPCount ;
      for ($m = 0; $m < $PoroCount ; $m++ ) {
      $RandO = $OilVolFac[array_rand($OilVolFac)];
       print "<li> $RandO <li>";
       }
        $arrayA = array($RandA) ;
        print '<br $arrayA[0] />' ;
        $arrayA1 = $arrayA[0] ;
        print $arrayA1 ;
        $arrayH = array($RandH) ;
        print '<br $arrayH[0] />' ;
        $arrayH1 = $arrayH[0] ;
        print $arrayH1 ;
        $arrayP = array($RandP) ;
        print '<br $arrayP[0] />' ;
        $arrayP1 = $arrayP[0] ;
        print $arrayP1 ;
        $arrayS = array($RandS) ;
        print '<br $arrayS[0] />' ;
        $arrayS1 = $arrayS[0] ;
        print $arrayS1 ;
       $arrayO = array($RandO) ;
       print '<br $arrayO[0]  />' ;
       $arrayO1 = $arrayO[0];  
       print '<br $arrayO1 />';
       $Sample = array(array($RandA), array($RandH), array($RandP), array($RandS),       `enter code her`array($RandO));
      print'<br $Sample[3][1] >' ;

      for ( $n = 0 ; $n < $PIIPCount; $n++) {
      $PIIPCalc = (($arrayA1*$arrayH1*$arrayP1)*(1-$arrayS1)/$arrayO1) ;
       print  round($PIIPCalc).  " " ;
       }
       ?>
       </body>
       </html>

变量是算法中的占位符。您不会"运行具有不同变量的算法",这意味着您更改了算法;否,您运行的算法的变量值不同

目前还不清楚您在此处尝试更改哪些变量,但请考虑以下情况:

function myAlgorithm($value) {
    // do something
    return $result;
}
echo myAlgorithm('foo');
echo myAlgorithm('bar');
echo myAlgorithm('baz');

myAlgorithm中的算法和变量不会更改,但您正在运行具有不同输入值的相同算法。我相信这就是你想要的;了解函数。

你可以对循环做同样的事情:

foreach (array('foo', 'bar', 'baz') as $value) {
    // do something
    echo $result;
}

您没有更改进行计算的底部 for 循环变量中的值。此外,当您循环访问顶部的数组以获取随机值时,您只是覆盖了$Rand{whatever}变量的值。整个事情可以通过单个循环来完成,例如:

<?php
$PIIPCount = 3 ;
$Area = array(1000, 1500, 2000, 2500, 3000) ;
$Height = array(100, 150, 200, 250, 250) ;
$Poro = array(0.05, 0.10, 0.15, 0.20) ;
$SatCon = array(0.02, 0.04, 0.06, 0.08) ;
$OilVolFac = array(1.05, 1.1, 1.15, 1.2) ;
//loop for count of PIIPCount
for($i=0; $i<$PIIPCount; $i++){
    //get one value from each array at random and save it on our blank arrays
    $randA = $Area[array_rand($Area)];
    $randH = $Height[array_rand($Height)];
    $randP = $Poro[array_rand($Poro)];
    $randS = $SatCon[array_rand($SatCon)];
    $randO = $OilVolFac[array_rand($OilVolFac)];
    $PIIPCalc = (($randA*$randH*$randP)*(1-$randS)/$randO) ;
    print "(($randA*$randH*$randP)*(1-$randS)/$randO) = ";
    print  round($PIIPCalc).  "<br>" ;
}

http://codepad.viper-7.com/FddttW

在 for 循环的每次迭代中,我都会将新的随机值放入我的$rand变量中,这些值正在公式中使用。