devWhiz Posted July 28, 2011 Share Posted July 28, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/243064-update-array-value-in-an-if-statement/ Share on other sites More sharing options...
phpSensei Posted July 28, 2011 Share Posted July 28, 2011 What in the world is that loop suposed to do? $x!=-1; Quote Link to comment https://forums.phpfreaks.com/topic/243064-update-array-value-in-an-if-statement/#findComment-1248368 Share on other sites More sharing options...
devWhiz Posted July 28, 2011 Author Share Posted July 28, 2011 $x!=-1 is an infinite loop.. its just a little project I am working on Quote Link to comment https://forums.phpfreaks.com/topic/243064-update-array-value-in-an-if-statement/#findComment-1248700 Share on other sites More sharing options...
Gabslol Posted July 28, 2011 Share Posted July 28, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/243064-update-array-value-in-an-if-statement/#findComment-1248731 Share on other sites More sharing options...
devWhiz Posted July 28, 2011 Author Share Posted July 28, 2011 $ratios[$id] = sprintf("%.16f", $value1 / $value2); that is what calculates the ratio, so I would have to place $ratios[$id] = sprintf("%.16f", $value1 / $value2); after the if statement to recalculate the ratio? Quote Link to comment https://forums.phpfreaks.com/topic/243064-update-array-value-in-an-if-statement/#findComment-1248764 Share on other sites More sharing options...
Gabslol Posted July 28, 2011 Share Posted July 28, 2011 Yes, give it a go and let us know. edit: I would actually put it inside the if statement, right after when value2 gets changed. That way, if the if() statement is not executed, it will not run the code again. Quote Link to comment https://forums.phpfreaks.com/topic/243064-update-array-value-in-an-if-statement/#findComment-1248789 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.