0

我有一个 PHP 函数,它执行以下操作,从 CSV 文件调用数据:

// 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

您在两个不同的地方分配给该变量,如下所示:

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

这将分配当前行列的值MaxDiff。如果您正在寻找整个 CSV 的最大最大差异,则必须比较这些值。像这样的东西:

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

(您必须在定义后 的某处添加它$maxdiff)。


更新- 所以上面不是你实际问题的解决方案

您通过对函数$maxdiff 的引用传递,因此实际上在函数运行后,它的值将是最后一行的值(因为您在进入for循环后立即从函数内部重新分配。要解决这个问题,请不要t 通过引用传递,通过&从函数签名中删除:

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