Verify which is the variable with greater value returning the variable not the value

3

I would like to know how to return the largest number variable but not the value but which one is larger.

For example:

$valor_01 = 6;
$valor_02 = 4;
$valor_03 = 3;
$valor_04 = 9;
$valor_05 = 8;
$valor_06 = 5;

echo max($valor_01, $valor_02, $valor_03, $valor_04, $valor_05, $valor_06);

// OUTPUT EXIBIDO
// 9

// OUTPUT DESEJADO PARA CONTROLE
// VAR $valor_04

I'm going to use this variable for controls if , but I need to know which is the largest variable and not the value.

If anyone knows how to put it together otherwise, thank you.

    
asked by anonymous 25.08.2015 / 14:44

4 answers

1

I do not know exactly what you need, but you can do this:

<?php

$valor_01 = 6;
$valor_02 = 4;
$valor_03 = 3;
$valor_04 = 9;
$valor_05 = 8;
$valor_06 = 5;

echo '$'.max(explode(', $','$valor_01, $valor_02, $valor_03, $valor_04, $valor_05, $valor_06'));

And in this case you need to capture the value of the largest variable, just use variable variant:

$var = max(explode(', $','$valor_01, $valor_02, $valor_03, $valor_04, $valor_05, $valor_06'));
echo $$var;

Only the ideal, when it comes to a collection, is not to make numerical use of variables. In this case, use array() :

$valor[1] = 6;
$valor[2] = 4;
$valor[3] = 3;
$valor[4] = 9;
$valor[5] = 8;
$valor[6] = 5;

//pega a maior índice
echo max(array_keys($valor));
//pega o valor do maior índice
echo $valor[max(array_keys($valor))];
    
25.08.2015 / 15:03
1

I believe you should control this as follows:

$array[0] = 2;
$array[1] = 7;
$array[2] = 3;
$array[3] = 4;
$array[4] = 5;

foreach($array as $key => $value) {
    if ($value > $val_max) {
        $key_max = $key;
        $val_max = $value;
    }
}

//Maior valor || maior array;
echo $array[$key_max];
    
25.08.2015 / 15:15
1
$array[0] = 6;
$array[1] = 4;
$array[2] = 3;
$array[3] = 9;
$array[4] = 8;
$array[5] = 5;

$index = -1
$max_value = $array[0];

for ($i = 1; $i <= count($array); $i++) {
    if($array[$i] > $max_value){
      $max_value = $array[$i];
      $index = $i;
    }
}

//aqui você tem o index do array onde está a variável com max_value
    
25.08.2015 / 15:33
0

See how this function works.

<?php

function ShowVar($var) {
    foreach($GLOBALS as $varName => $value) {
        if ($value === $var) {
            return $varName;
        }
    }
    return false;
}

$valor_01 = 6;
$valor_02 = 4;
$valor_03 = 3;
$valor_04 = 9;
$valor_05 = 8;
$valor_06 = 5;

$result = max($valor_01, $valor_02, $valor_03, $valor_04, $valor_05, $valor_06);

echo '$'.ShowVar($result);
  

Return

  $ value_04

    
25.08.2015 / 15:08