2012-10-03 49 views
0

我有一个PHP函数,它的下面,从一个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文件中最后一行的值。任何帮助将不胜感激。

回答

1

分配给该变量在两个不同的地方,像这样:

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

这就赋予当前行的MaxDiff列的值。如果您在整个CSV中查找最高的最大差异,则必须比较这些值。事情是这样的:

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

(你要添加的地方$maxdiff已定义)。


UPDATE - 所以上面是不是解决您的实际问题

你的函数合格后$maxdiff参照的功能,所以实际上运行它的价值将是从最后一行开始(因为您从函数内部重新分配,在您输入for循环之后)。要解决该问题,只需通过从函数签名中删除&即可:

function calculateNPVByWAL ($paymentRowArray, $debugMode, $isLow, $maxdiff) { 
    // function body can stay the same 
} 
+0

如果我不清楚,我很抱歉。是的,我意识到我把它分配在两个地方,但我绝望了,我不知道哪个是正确的。但是在函数的“回显”表中,出现了正确的MaxDiff。但是,当我在第二段代码中调用它时,它不会从CSV文件中的正确行返回数字。我希望它选择出现在“回显”表中的相同信息,而不是整个CSV中的最高MaxDiff。 (更清楚了吗?)谢谢。 –

+0

好的,看我的更新 – bfavaretto

+0

非常感谢你的帮助。非常感激。 –