devWhiz Posted May 22, 2011 Share Posted May 22, 2011 Ok so say I have php do some calculations for me $Value1 = 300; $Value2 = 100000; $FirstRatio = $Value1 / $Value2; $Value3 = 8000; $Value4 = 6000000; $SecondRatio = $Value3 / $Value4; $FirstRatio = 0.003 $SecondRatio = 0.0013 How would I go about doing these calculations and then sorting $FirstRatio and $SecondRatio and then it would take the highest ratio ($FirstRatio) and it would echo the value? Quote Link to comment https://forums.phpfreaks.com/topic/237116-sort-decimals-least-to-greatest-and-vice-versa/ Share on other sites More sharing options...
wildteen88 Posted May 22, 2011 Share Posted May 22, 2011 Use an if which compares the two variables if($FirstRatio > $SecoundRatio) echo $FirstRatio; else echo $SecoundRatio; Quote Link to comment https://forums.phpfreaks.com/topic/237116-sort-decimals-least-to-greatest-and-vice-versa/#findComment-1218683 Share on other sites More sharing options...
devWhiz Posted May 22, 2011 Author Share Posted May 22, 2011 Im going to PM you wildteen Quote Link to comment https://forums.phpfreaks.com/topic/237116-sort-decimals-least-to-greatest-and-vice-versa/#findComment-1218684 Share on other sites More sharing options...
wildteen88 Posted May 22, 2011 Share Posted May 22, 2011 Im going to PM you wildteen No point I don't read PM's. If you need help post it in the forums. Quote Link to comment https://forums.phpfreaks.com/topic/237116-sort-decimals-least-to-greatest-and-vice-versa/#findComment-1218685 Share on other sites More sharing options...
devWhiz Posted May 22, 2011 Author Share Posted May 22, 2011 Well I will have 20+ ratios I need to sort, I need to be able to calculate all of the ratios and then echo the highest one.. IE; Ratio 1 = 0.00023 Ratio 2 = 0.00025 Ratio 3 = 0.00043 Ratio 3 is the highest so I will need to put that into a variable and then echo it out Quote Link to comment https://forums.phpfreaks.com/topic/237116-sort-decimals-least-to-greatest-and-vice-versa/#findComment-1218688 Share on other sites More sharing options...
wildteen88 Posted May 22, 2011 Share Posted May 22, 2011 Basically add all the ratios your script calculates into an array then use max which will pick the highest ratio from the array Quote Link to comment https://forums.phpfreaks.com/topic/237116-sort-decimals-least-to-greatest-and-vice-versa/#findComment-1218691 Share on other sites More sharing options...
devWhiz Posted May 22, 2011 Author Share Posted May 22, 2011 Hmm, Ok how about this, I guess I should have explained a little but more my bad Im tryin to game a calculator for this game to buy the best bang for buck property (lol), as lame as it sounds, I make money making tools for this game so w/e So there is property in the game, I use simplexml to parse out the cost and income and then I calculate the ratios, well I want to be able to buy the property that has the best ratio, I just need to figure out how to define the best ratio and then have the script know what property it is that has the best ratio, and then buy the property, a url buys the property so I would use file_get_contents Quote Link to comment https://forums.phpfreaks.com/topic/237116-sort-decimals-least-to-greatest-and-vice-versa/#findComment-1218696 Share on other sites More sharing options...
wildteen88 Posted May 22, 2011 Share Posted May 22, 2011 Without seeing your code how you are getting the cost/income and calculating the ratio I cannot provide you a better answer other than what I posted above. This is what you will need to do add all the ratios your script calculates into an array then use max which will pick the highest ratio from the array Quote Link to comment https://forums.phpfreaks.com/topic/237116-sort-decimals-least-to-greatest-and-vice-versa/#findComment-1218698 Share on other sites More sharing options...
devWhiz Posted May 22, 2011 Author Share Posted May 22, 2011 <?php $XML = simplexml_load_string("property_list.xml"); foreach($XML->xml as $value) { if($value->id == 10) { $id10_cost=$value->cost; $id10_income=$value->income; $id10_ratio = $id10_income / $id10_cost; } else if($value->id == 12) { $id12_cost=$value->cost; $id12_income=$value->income; $id12_ratio = $id12_income / $id12_cost; } else if ($value->id == 16) { $id16_cost=$value->cost; $id16_income=$value->income; $id16_ratio = $id16_income / $id16_cost; } } ?> Thats shortened, but thats my code to parse out and get the ratios, I could use max() to find the highest ratio but I don't know how I could define the highest ratio in a variable and then match it up with the ratios to see what property ID it would be that I would have to purchase the property. sorry if I am being confusing Quote Link to comment https://forums.phpfreaks.com/topic/237116-sort-decimals-least-to-greatest-and-vice-versa/#findComment-1218708 Share on other sites More sharing options...
wildteen88 Posted May 22, 2011 Share Posted May 22, 2011 So you only want to calculate the ratios with the properties that have the id of 10, 12 and 16? Or do you want to do it for all properties within property_list.xml? Quote Link to comment https://forums.phpfreaks.com/topic/237116-sort-decimals-least-to-greatest-and-vice-versa/#findComment-1218709 Share on other sites More sharing options...
devWhiz Posted May 22, 2011 Author Share Posted May 22, 2011 all of the properties within the xml file I just listed 3 to shorten it up a bit, I need it to load the file, calculate the best ratio, buy the property, reload the file to check for the best ratio among properties again and repeat Thanks for helpin me wildteen Quote Link to comment https://forums.phpfreaks.com/topic/237116-sort-decimals-least-to-greatest-and-vice-versa/#findComment-1218713 Share on other sites More sharing options...
wildteen88 Posted May 22, 2011 Share Posted May 22, 2011 Rather than assign each properties values to seperate variables ($id10_code $id10_value $id10_id etc). You'll want to assign them to an array instead within your foreach loop $key = $value->id; $properties[ $key ]['name'] = $value->name; $properties[ $key ]['cost'] = $value->cost; $properties[ $key ]['income'] = $value->income; After calculate the ratio and add it to a separate array for storing the ratios. When adding the ratio to the array assign its key as the property id eg $ratios[ $key ] = $value->income / $value->cost; Now after the foreach loop you then use max to get the highest ratio from the $ratios array. $highestRatio = max($ratios); // get the highest ratio Next use array_search to get property details for the highest ratio. $propertyKey = array_search($highestRatio, $ratios); // get the property key $property = $properties[ $propertyKey ]; // get the property with the highest ratio $property will be associative array of the properties details (name, cost, income). Complete code $XML = simplexml_load_string("property_list.xml"); $ratios = array(); $properties = array(); foreach($XML->xml as $value) { $key = $value->id; $properties[ $key ]['name'] = $value->name; $properties[ $key ]['cost'] = $value->cost; $properties[ $key ]['income'] = $value->income; $ratios[ $key ] = $value->income / $value->cost; } $highestRatio = max($ratios); // get the highest ratio $propertyKey = array_search($highestRatio, $ratios); // get the properties ID $property = $properties[ $propertyKey ]; // get the property with the highest ratio echo $property['name'] . ' has the best ratio of ' . $highestRatio; Quote Link to comment https://forums.phpfreaks.com/topic/237116-sort-decimals-least-to-greatest-and-vice-versa/#findComment-1218719 Share on other sites More sharing options...
devWhiz Posted May 22, 2011 Author Share Posted May 22, 2011 ok, I think Im gonna have to modify the code a little bit, Im gonna work on it and let you know how it turns out, and if I need anymore help, ty for takin to time ot put that code together for me Quote Link to comment https://forums.phpfreaks.com/topic/237116-sort-decimals-least-to-greatest-and-vice-versa/#findComment-1218735 Share on other sites More sharing options...
devWhiz Posted May 22, 2011 Author Share Posted May 22, 2011 ahh seen you edited it, about to check it out ty, I was having a few problems with the other code Just figured I kind of messed something up Quote Link to comment https://forums.phpfreaks.com/topic/237116-sort-decimals-least-to-greatest-and-vice-versa/#findComment-1218737 Share on other sites More sharing options...
wildteen88 Posted May 22, 2011 Share Posted May 22, 2011 Yea sorry, I posted in the wrong code earlier. I tried to edit it before you read my reply. Quote Link to comment https://forums.phpfreaks.com/topic/237116-sort-decimals-least-to-greatest-and-vice-versa/#findComment-1218743 Share on other sites More sharing options...
devWhiz Posted May 22, 2011 Author Share Posted May 22, 2011 Doesnt seem like the arrays are holding any data.. <?php $XML = simplexml_load_file("get_city_list.xml"); $ratios = array(); $properties = array(); foreach($XML->xml->establishments->land as $value) { $key = $value->id; $properties[$key]['cost'] = $value->cost; $remove = str_replace('$', '', $value->income); $income = str_replace(',', $remove); $properties[$key]['income'] = $income; $ratios[$key] = $income / $value->cost; } $highestRatio = max($ratios); // get the highest ratio $propertyKey = array_search($highestRatio, $ratios); // get the properties ID $property = $properties[$propertyKey]; // get the property with the highest ratio echo $property['name'] . ' has the best ratio of ' . $highestRatio; sleep(10000); ?> var_dump() and print_r() both show that there is no data in the ratios array if I echo out $key."\n" it prints the IDs of the property in the XML file just fine.. I'm not real sure where something went wrong, and there is no ['name'] value in the xml but that shouldnt have caused any issues Quote Link to comment https://forums.phpfreaks.com/topic/237116-sort-decimals-least-to-greatest-and-vice-versa/#findComment-1218753 Share on other sites More sharing options...
wildteen88 Posted May 22, 2011 Share Posted May 22, 2011 I didn't test my code using XML data. I used some hard coded values. However this shouldn't make a difference. Could I see how your XML file is constructed? Quote Link to comment https://forums.phpfreaks.com/topic/237116-sort-decimals-least-to-greatest-and-vice-versa/#findComment-1218758 Share on other sites More sharing options...
devWhiz Posted May 22, 2011 Author Share Posted May 22, 2011 <outer> <xml> <establishments> <land> <id>24</id> <cost>920000000000</cost> <income>$100,000,000</income> <name>NAME</name> </land> <land> <id>17</id> <cost>921000</cost> <income>$100</income> <name>NAME</name> </land> <land> <id>3</id> <cost>2763000</cost> <income>$300</income> <name>NAME</name> </land> </establishments> </xml> </outer> way more entries than that though, 20+, I just cut it down some, and I was wrong, name is in the XML Quote Link to comment https://forums.phpfreaks.com/topic/237116-sort-decimals-least-to-greatest-and-vice-versa/#findComment-1218760 Share on other sites More sharing options...
wildteen88 Posted May 22, 2011 Share Posted May 22, 2011 The only way I got it to work is to use type casting. Change the foreach loop to foreach($XML->xml->establishments->land as $value) { $key = (int) $value->id; $cost = (int) $value->cost; $income = (int) str_replace(array('$', ','), '', (string) $value->income); $properties[ $key ]['cost'] = $cost; $properties[ $key ]['income'] = $income; $ratios[ $key ] = $income / $cost; } Quote Link to comment https://forums.phpfreaks.com/topic/237116-sort-decimals-least-to-greatest-and-vice-versa/#findComment-1218769 Share on other sites More sharing options...
devWhiz Posted May 22, 2011 Author Share Posted May 22, 2011 Man, Youre awesome bro! I really appreciate the help! One more thing What would I have to do to, get the script to output all of the ratios in order like... Buy $Name first ::: RATIO : $ratio ::: Buy $Name second ::: RATIO : $ratio ::: Buy $Name third ::: RATIO : $ratio ::: Buy $Name fourth ::: RATIO : $ratio ::: Buy $Name fifth ::: RATIO : $ratio ::: Buy $Name sixth ::: RATIO : $ratio ::: I don't expect you to help me with that, If you could maybe point me in the right direction, You've already went out of your way to help me with what you have so far, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/237116-sort-decimals-least-to-greatest-and-vice-versa/#findComment-1218786 Share on other sites More sharing options...
wildteen88 Posted May 22, 2011 Share Posted May 22, 2011 Use arsort($ratios); instead of $highestRatio = max($ratios);. Then use a while/each loop (look at example 2) to out out each property. EDIT Or just use a foreach loop on the $ratios array, eg foreach($ratios as $propertyKey => $ratio) Quote Link to comment https://forums.phpfreaks.com/topic/237116-sort-decimals-least-to-greatest-and-vice-versa/#findComment-1218787 Share on other sites More sharing options...
devWhiz Posted May 22, 2011 Author Share Posted May 22, 2011 Sweet, I will work at that, ok ONE last thing and Ill stop buggin you, how would ignore a few ids? Like say I didnt want it to calculate the ratio for 22 or, 10, or 6, just examples but what could I use to block those out, and when I get paid I will throw you a few dollars on paypal if you want for helping me out Quote Link to comment https://forums.phpfreaks.com/topic/237116-sort-decimals-least-to-greatest-and-vice-versa/#findComment-1218794 Share on other sites More sharing options...
Zane Posted May 22, 2011 Share Posted May 22, 2011 just put an IF statement in your FOREACH and use continue to skip it. foreach($status as $propertyKey=>$ratio) { if($propertyKey == 22 OR $propertyKey == 10 OR $propertyKey == 6) continue; } Quote Link to comment https://forums.phpfreaks.com/topic/237116-sort-decimals-least-to-greatest-and-vice-versa/#findComment-1218796 Share on other sites More sharing options...
xyph Posted May 22, 2011 Share Posted May 22, 2011 An alternate way: $not = array(22,10,6); foreach($status as $propertyKey=>$ratio) { if( in_array($propertyKey,$not) ) continue; } Quote Link to comment https://forums.phpfreaks.com/topic/237116-sort-decimals-least-to-greatest-and-vice-versa/#findComment-1218802 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.