2012-10-03 85 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

非常感謝你的幫助。非常感激。 –