devWhiz Posted July 31, 2011 Share Posted July 31, 2011 how would I reference the array inside of the foreach parameters, like this function CalculateRatios(&$ValueArray){ foreach($ValueArray as $ID => $ValueInfo){ $Value1 = $ValueInfo['Value1']; $Value2 = $ValueInfo['Value2']; $Ratios[$ID] = sprintf("%.16f", $Value1 / $Value2); } $BestRatio = max($Ratios); $BestID = array_search($BestRatio, $Ratios); return array($BestID, $BestRatio); } I have the ampersand in the function parameters, but since the array name changes in the foreach parameters, do I need to put an ampersand in front of the changed name? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 31, 2011 Share Posted July 31, 2011 The following line passes the variable you pass to it by reference. function CalculateRatios(&$ValueArray){ passing by reference is explained in the manual here http://php.net/manual/en/language.references.pass.php What is it you are trying to do? Quote Link to comment Share on other sites More sharing options...
devWhiz Posted July 31, 2011 Author Share Posted July 31, 2011 Im just having some trouble with this script, you actually helped me understand what it is that needed done when I first attempted to write the script months ago, Im having issues though... Let me try to explain I have an array like so $Values[15]['Value1'] = 15; $Values[15]['Value2'] = 1000; $Values[15]['Value3'] = 15000; $Values[15]['Value4'] = 15000; $Values[15]['Value5'] = 4000; $Values[15]['Value6'] = 0; $Values[5]['Value1'] = 5; $Values[5]['Value2'] = 3000; $Values[5]['Value3'] = 90000; $Values[5]['Value4'] = 90000; $Values[5]['Value5'] = 45000; $Values[5]['Value6'] = 0; The script calculates the best ratio for each element in the array, the ratio is determined by $Ratios[$ID] = sprintf("%.16f", $Value2 / $Value3); it then gets the ID that is associated with whatever element has the best ratio, so $Value[15]['Value2'] equals 1000 and $Value[15]['Value3'] equals 15000 the ratio would equal 0.6666666667.. the ratio for ID 5 = 0.033333333, I use max to find the highest ratio, then array search to get the ID associated with the highest ratio now this is the problem.. I use a for loop to increment the values in the array like this [/code] for($x=0; $x!=-1; $x++){ $Values[$BestID]['Value2'] += $Values[$BestID]['Value4']; $Values[$BestID]['Value2'] += $Values[$BestID]['Value5']; $Values[$BestID]['Value6'] += 10; } [/code] every time it loops, it increments value2 with value4, then takes the new value2 and adds value5, and value6 increments by 10 each loop.. well, what I want to do is, if value6 is under 10, then value2 -= value5.. I get value2 to show up correctly, but the ratio array doesnt update.., it loops, updates value2, but it doesnt update the ratio for the new cost when value6 is under 10.. This probably makes no sense.. when I try to explain, I confuse myself, let me know if I have to better explain Thank you Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.