Help sorting variables by value

Associate
Joined
28 Apr 2007
Posts
27
Location
Colchester, UK
I'm currently using an IF statement to find out which variable I have is the highest by comparing each variable to each other, but this only works as long as 2 aren’t the highest and the same value. I need to be able to find out which variable is the highest value, and if there is more than 1, which 2 or 3 etc are the highest. I'm not really sure the best approach for this, maybe put the variables into an array and sort the array? But I can't get my head around how to find out if there are 2 the same value that are highest.

For example:
Code:
$var1 = 5;
$var2 = 5;
$var3 = 4;
$var4 = 2;
With something like that, how can I find which variable is the highest?

Any help with this appreciated :)
 
Last edited:
Not had a lot of time to think about this but look into variable variables to loop around all your variables and add each variable with the highest value into an array.

Job Done.
 
Code:
$vars = array(5, 5, 4, 2);

sort($vars, SORT_NUMERIC);

$highest = max($vars);

echo $highest;

or if you want to use $var1, $var2 as you put it ( which is a bad idea )

try this

Code:
$var1 = 5;
$var2 = 5;
$var3 = 4;
$var4 = 2;
$i = 1;

	while (isset(${"var" .$i})) {
		$vars[] = ${"var" . $i};
		$i++;
	}

sort($vars, SORT_NUMERIC);

$highest = max($vars);

echo $highest;

Hope you enjoy being spoon fed :p
 
Last edited:
Back
Top Bottom