PHP 函数 - 函数的输出是另一个函数的输入


PHP function - output of function is the input of another function

我有以下函数,其中给出了某些输入,然后给出了4个输出: -

function rsiNext($dailyGainAvgPrev, $dailyLossAvgPrev,$cpDailyNext){
    if($cpDailyNext > 0){
        $dailyGainAvgNext = (($dailyGainAvgPrev * 13) + $cpDailyNext)/14;
    }else{
        $dailyGainAvgNext = (($dailyGainAvgPrev * 13) + 0)/14;
    }
    if($cpDailyNext < 0){
        $dailyLossAvgNext = (($dailyLossAvgPrev*13) + abs($cpDailyNext))/14;
    }else{
        $dailyLossAvgNext = (($dailyLossAvgPrev*13) + abs(0))/14;
    }
    $relStrNext = $dailyGainAvgNext/$dailyLossAvgNext;
    if($dailyLossAvgNext == 0){
        $relStrIndNext = 100;
    }else{
        $relStrIndNext = 100-(100/(1+$relStrNext));
    }
    return array($dailyGainAvgNext, $dailyLossAvgNext, $relStrNext, $relStrIndNext);
}

我使用以下代码行输出值:

//Get value for day 15
list($dailyGainAvg02, $dailyLossAvg02, $relStr02, $relStrInd02) = rsiNext($averageGains14, $averageLosses14, $priceDifferences[15]);
echo '<tr><td>'.$dailyGainAvg02.'</td><td>'.$dailyLossAvg02.'</td><td>'.$relStr02.'</td><td>'.$relStrInd02.'</td></tr>';

现在,当我想要第 16 天的值时,我使用以下代码行:

//Get value for day 16
list($dailyGainAvg03, $dailyLossAvg03, $relStr03, $relStrInd03) = rsiNext($dailyGainAvg02, $dailyLossAvg02, $priceDifferences[16]);
echo '<tr><td>'.$dailyGainAvg03.'</td><td>'.$dailyLossAvg03.'</td><td>'.$relStr03.'</td><td>'.$relStrInd03.'</td></tr>';
第 15 天的输出是第 16 天的输入

,第 16 天的输出是第 17 天的输入。第 17 天的输出是第 18 天的输入,依此类推...

我需要重复该列表 100 天。我怎样才能在不再重复 100 天list行的情况下做到这一点?

谢谢。

假设您完全填充了$priceDifferences数组,应该执行以下操作:

$cur_dailyGainAvg = 0; // you need to initialize this value appropriately
$cur_dailyLossAvg = 0; // you need to initialize this value appropriately
for ($idx = 1; $idx <= 100; $idx++) {
  list($new_dailyGainAvg, $new_dailyLossAvg, $new_relStr, $new_relStrInd) = rsiNext($cur_dailyGainAvg, $cur_dailyLossAvg, $priceDifferences[$idx])
  // print
  echo '<tr><td>'.$new_dailyGainAvg.'</td><td>'.$new_dailyLossAvg.'</td><td>'.$new_relStr.'</td><td>'.$new_relStrInd.'</td></tr>';
  // shift the new values onto the current, and repeat the calculation
  $cur_dailyGainAvg = $new_dailyGainAvg;
  $cur_dailyLossAvg = $new_dailyLossAvg;
} 
基本上区分你输入到函数中的"当前"值和产生的"新"值

,然后将新值"转移"到当前值上并重复。

您可能需要检查循环的边界。