How to return the VARIABLE with the highest integer value

So I took this function from another forum, and I thought all was working well, however it seems it chooses the variable you refer to first:

function max_value() 
	{
   	 $args = func_get_args();

    $values = array();
    foreach ($args as $arg) { global $$arg; $values["$arg"] = $$arg; }
    foreach ($values as $key => $value) if ($value == max($values)) return $key;
  }

So for instance if you have $var1, $var2, $var3, the concept is you enter max_value(‘var1’,‘var2’,‘var3’) and it returns the variable name with the highest integer.

However the function is a bit beyond me and I seem to have the need for a function that does such, perhaps since this function was written in a forum 2 years ago some of the functionality has been changed?

Any assistance on this would be greatly appreciated, thanks

Hmm, I don’t know if that’s possible since the argument variables are dynamic in the function with get_args. If you were using a certain amount of placeholders you could reference them with


function maxim(&$a, &$b)
{}

A better way to get max #


echo max(1, 22, 44);

My toying around


function maxim()
{
	$args = &func_get_args();
	rsort($args);
	echo $args[0];
	print_r($args);
}

one way of doing it is to pass the names of the parameters as well to the function

 
<?php
 
function max_value($args) {
    $max_value = max($args);
    //now get the key for the first occurence of max value
    foreach ($args as $key => $value) {
        if ($value == $max_value) {
            return $key;
        }
    }
}
 
//test the function
 
$params = array(
    '$var1' => 33,
    '$var2' => 26,
    '$var3' => 114);
 
echo 'Max value is in ' . max_value($params);
 
?>

Why not have the calling code pass in an array of values you want to test


function max_value($values) {
   $max = 0;
   foreach($values as $v) {
      if($v > $max) $max = $v;
   }
   return $max;
}

Then your calling script might look like this:


$var1 = 7;
$not_tested = 3;
$var2 = 20;
$var3 = 9;
$another = 'lorem ipsum';

echo max_value(array($var1, $var2, $var3));

or if you want both the max value and the variable name you could do something similar to this

 
<?php
function max_value($args) {
    $results = array();
    $max_value = max($args);
    $results[] = $max_value;
 
    //now get the key for the first occurence of max value
    $found_key = false;
    foreach ($args as $key => $value) {
        if ($value == $max_value && !$found_key) {
            $results[] =  $key;
            $found_key = true;
        }
    }
    return $results;
}
 
//test the function
$params = array(
    '$var1' => 33,
    '$var2' => 26,
    '$var3' => 114);
 
$results = max_value($params);
 
echo 'Max value = '.$results[0].' and is in ' . $results[1];
 
?>

If you need the variable name, and there aren’t too many values, you might be able to just do this:



$max_var = NULL;

foreach ( array( 'var1', 'var2', 'var3', ... ) as $var_name ) {

  if ( $$var_name > $$max_var ) {

    $max_var = $var_name;

  }
  // if

}
// foreach


Otherwise you could one way or another create an array where the variable names are the keys and the variable values are the array values, then do this:



asort( $number_vars ); 

$max_value = end( $number_vars );

$max_var = key( $number_vars );