Verifies which variable is with higher value returning the variable not the value

Asked

Viewed 2,897 times

3

I would like to know how to return the variable of larger number but not the value and yes which 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 will 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 ride even otherwise, thank you.

  • 2

    explain better the need to know the variable name

  • 3

    Your problem probably already starts by using several variables instead of one array. Learn to use the right resource and you won’t have to deal with it. You get nothing by doing it, not even learning.

  • Moisesgame, I gave you the answer you need. That’s what you asked. But depending on what you need to do in your application, you can use another method. In that case you could explain to us what you need and we can give you alternatives. I learned from this question, even if out of curiosity, how to print the name of a variable. Will I use a day? I don’t know. But it’s good to know. And surely you get learning too.

  • @Pedrolaini I want the return of the larger variable and not the value. If the variable $valor_04 is the biggest. I want to use it later on a if for example if ($variavel_maior > $variavel_atual) . I tried to be clearer =]

  • Moises, by your answer of now, it is possible for you to do otherwise what you want, other than by that way which is not the most recommended.

  • @Diegosouza how could I do? I’m doing so because these values come from queries in the bank, one of each different query. If you could help me, I’d really appreciate it.

  • How these values come from the database ?

  • @Diegosouza There are 6 separate queries, each of a date. Each query returns 2 different values that are summed into a variable. An example $total_data_01 = ( round( $dados_data_01["LIGACOES"] * 0.7 ) + round( $dados_data_01["CONTATOS"] * 0.8 ) );

  • @Diegosouza so use in variables, I have to treat their values later.

  • @Moisesgama the only correct solutions to your problem is Pedro Laini and Daniel Gregatto. Learn to do it the right way. If there is something he can improve on, put a comment in his answer, but don’t adopt wrong solutions. Although they could be simplified.

  • 1

    @Moisesgama I answered your question for the purpose you need, but in case of collections or sequences, I recommend using array, not only for the use of memory, but for the advantage of organization and understanding of the code.

  • @Ivanferrer Your response has met your needs. Thank you very much! You commented to use array how his response could adapt into a array , could you make an example in your reply? Thank you.

Show 7 more comments

4 answers

1


I don’t know exactly what you need it for, but you can do it:

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

But the ideal thing 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))];
  • Thanks Ivan, your response helped a lot!

1

I believe you should control it this way:

$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];
  • This returns 7, i.e., the value and not the key of the array. Knows how to return the key?

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

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

$valor_04

  • Thank you Diego, I’ll save your job thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.