Jump to content

Recommended Posts

Here is the script

 


<?php

$array[0]['id'] = 0;
$array[0]['value1'] = 1000;
$array[0]['value2'] = 15000;
$array[0]['value3'] = 15000;
$array[0]['value4'] = 0;

$array[1]['id'] = 1;
$array[1]['value1'] = 500;
$array[1]['value2'] = 3300;
$array[1]['value3'] = 3300;
$array[1]['value4'] = 0;

$array[2]['id'] = 2;
$array[2]['value1'] = 250;
$array[2]['value2'] = 3500;
$array[2]['value3'] = 3500;
$array[2]['value4'] = 0;

$array[3]['id'] = 3;
$array[3]['value1'] = 800;
$array[3]['value2'] = 2500;
$array[3]['value3'] = 2500;
$array[3]['value4'] = 0;

for($x=0; $x!=-1; $x++){
	$random = rand(0,3);
	foreach($array as $arrays){
		$id = $arrays['id'];

		$value1 = $arrays['value1'];
		$value2 = $arrays['value2'];		
		$value3 = $arrays['value3'];		
		$value4 = $arrays['value4'];	

		$newarray[$id]['value1'] = $value1; // income
		$newarray[$id]['value2'] = $value2; // cost
		$newarray[$id]['value3'] = $value3; // static.. init cost	
		$newarray[$id]['value4'] = $value4; // owned

		$ratios[$id] = sprintf("%.16f", $value1 / $value2);
		print_r($ratios);
	}


$bestratio = max($ratios);
$idforbestratio = array_search($bestratio, $ratios);


$array[$idforbestratio]['value2'] += $array[$idforbestratio]['value3'];
if($array[$idforbestratio]['value4'] < 10){
	$array[$idforbestratio]['value2'] -= $array[$idforbestratio]['value3'];
	}
echo $array[$idforbestratio]['value2']."\n";


$array[$idforbestratio]['value4'] += 10;
sleep(1);
}

 

the script calculates the best ratio for each array entry

 

ratios are as follows

Array ( [0] => 0.0666666666666667 [1] => 0.1515151515151515 [2] => 0.0714285714285714 [3] => 0.3200000000000000 )

 

since $array[3] has the highest ratio, execute

 


$array[$idforbestratio]['value2'] += $array[$idforbestratio]['value3'];

 

so now $array[3]['value2'] would equal 5000

 

but I put the if statement in to change the value2 back to 2500 if value4 is under 10, it changes the value but the ratio doesnt update.. I dont get what I am doing wrong or what I would have to do to get the ratio to update.. ive tried to make the array value super global but it still refuses to update the ratio the next loop

 

any ideas on what I could do to get this to update the ratio value?

Link to comment
https://forums.phpfreaks.com/topic/243064-update-array-value-in-an-if-statement/
Share on other sites

Hello,

 

I can't see exactly which lines in there are calculating the ratio, but did you recalculate it after changing the value in the if statement?

 

Ex.

 

If you sort() an array, then add more elements in the array, you will need to sort it again.

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.