Jump to content

Recommended Posts

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?

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

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

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

<?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

 

 

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

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;

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

<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

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;
}

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!

 

 

 

 

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)

 

 

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

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.