Jump to content

foreach reference inside of a function


devWhiz

Recommended Posts

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?

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.