0

CSVファイルからデータを呼び出して、次のことを行うPHP関数があります。

// Calculates the Net Present Value based on the WAL and payment stream.

function calculateNPVByWAL ($paymentRowArray, $debugMode, $isLow, &$maxdiff) {
// Calculate the WAL.
$currentWAL = calculateWAL ($paymentRowArray, $debugMode);
// Get the Low/High rate table.
$csvdata = csv_in_array ('rate_data.csv', ",", "\"", true);
// The current rate.
$currentRate = 0;
// The last line of the low/high rate table used.
$lastLineUsed = 0;

// Loop through the Low/Rate WAL table.
for ($i = 0; $i < count ($csvdata); $i++) {
    // The max diff.
    $maxdiff = $csvdata [$i]['MaxDiff'];

    if ($isLow) {
        if ($currentWAL >= $csvdata[$i]['WAL']) {
            // The WAL is higher the current rate table row.
            $currentRate = $csvdata [$i]['Low'];
            // Store the last line used.
            $lastLineUsed = $i;
            //Set the offer cost and the offer profit
            $offer_cost = 3000;
            $offer_profit = 3000;
            //Get the max difference
            $MaxDiff = $csvdata [$i]['MaxDiff'];
        }
    } else {
        if ($currentWAL >= $csvdata[$i]['WAL']) {
            // The WAL is higher the current rate table row.
            $currentRate = $csvdata [$i]['High'];
            // Store the last line used.
            $lastLineUsed = $i;
        }
    }
}

// Setup the NPV value array.
$npvValueArray = array();

// Loop through the payment stream.
for ($i = 0; $i < count ($paymentRowArray); $i++) {

    // Add the payment stream cash flow.
    $npvValueArray[] = $paymentRowArray[$i]->getCashFlow();
}

if ($debugMode) {

    echo '<table border="2">';
    echo '<tr><td><strong>WAL Bracket</strong></td>';
    echo '<td align="right">' . $lastLineUsed . '</td></tr>';
    echo '<tr><td><strong>Low/High</strong></td>';
    echo '<td align="right">' . ($isLow ? 'Low' : 'High') . '</td></tr>';
    echo '<tr><td><strong>Rate</strong></td>';
    echo '<td align="right">' . $currentRate . '</td></tr>';
    echo '<tr><td><strong>NPV</strong></td>';
    echo '<td align="right">' . round (npv (($currentRate / 12 / 100), $npvValueArray), 2) . '</td></tr>';
    $highvalue = round (npv (($currentRate / 12 / 100), $npvValueArray), 2);

    if ($isLow) {
        $lowvalue = (round (npv (($currentRate / 12 / 100), $npvValueArray), 2) - $offer_cost - $offer_profit);
        echo '<tr><td><strong>Cost of Funds</strong></td>';
        echo '<td align="right">' . $offer_cost . '</td></tr>';
        echo '<tr><td><strong>Profit</strong></td>';
        echo '<td align="right">' . $offer_profit . '</td></tr>';
        echo '<tr><td><strong>Adjusted NPV</strong></td>';
        echo '<td align="right">' . (round (npv (($currentRate / 12 / 100), $npvValueArray), 2) - $offer_cost - $offer_profit) . '</td></tr>';
        echo '<tr><td><strong>Max Difference</strong></td>';
        echo '<td align="right">' . $MaxDiff . '</td></tr>';
    }

    echo '</table><br/>';

} else {


}


// Return the Net Present Value.
return (round (npv (($currentRate / 12 / 100), $npvValueArray), -2));
//return ($maxdiff);

}

次に、次のコードを使用して操作できる変数を作成できます。

$lowRate = calculateNPVByWAL ($paymentRowArray, $debugMode, TRUE, &$maxdiff);
echo 'The low rate value is $' . $lowRate .'<br>';
echo 'The maximum difference value is $' . $maxdiff .'<br>';
$adjlowRate = $lowRate - $offer_cost - $offer_profit;
echo 'The adjusted low rate value is $' . $adjlowRate .'<br>';
$highRate = calculateNPVByWAL ($paymentRowArray, $debugMode, FALSE);
echo 'The high rate value is $' . $highRate .'<br>';
$difference = $adjlowRate - $highRate;
echo 'The difference is $' . $difference .'<br>';

私の問題は、 $maxdiff 変数が正しい値を呼び出さないことです。CSVファイルの最終行から値を呼び出しているようです。どんな助けでも大歓迎です。

4

1 に答える 1

1

次のように、2 つの異なる場所でその変数に代入します。

$maxdiff = $csvdata [$i]['MaxDiff'];

これにより、現在の行の列の値が割り当てられMaxDiffます。CSV 全体で最大の最大差分を探している場合は、値を比較する必要があります。このようなもの:

$realMaxDiff = 0;
if($maxdiff > $realMaxDiff) {
    $realMaxDiff = $maxdiff;
}

(定義した後 のどこかに追加する必要があります)。$maxdiff


更新- したがって、上記は実際の問題の解決策ではありませんでした

$maxdiff 関数への参照渡しを行っているため、実際には関数が実行された後、その値は最後の行の値になります (関数内から再割り当てするため、forループに入った直後です。それを修正するには、「しないでください」&関数シグネチャからを削除することにより、参照渡しを行います。

function calculateNPVByWAL ($paymentRowArray, $debugMode, $isLow, $maxdiff) {
   // function body can stay the same
}
于 2012-10-03T21:48:48.490 に答える