Find out less value that is coming from an excel

Asked

Viewed 18 times

0

I have a spreadsheet that receives several values on the same line, they can both be filled or only one of the values filled. So for example:

Coluna                 |   A    |   B   |   C     |
Valor                  |  56,00 | 48,00 |  50,00  |  (menor valor = 48,00)
Valor                  |  10,00 |       |         |  (menor valor = 10,00)
Valor                  |  20,00 |       |  22,00  |  (menor valor = 20,00)
Valor                  |        | 15,00 |  17,00  |  (menor valor = 15,00)

Where empty space is empty even in excel.

I tried to take the lowest value in the following ways

  $valor_coluna_a = $field[0]; //equivale a coluna A
  $valor_coluna_b = $field[1]; //equivale a coluna B
  $valor_coluna_c = $field[2]; //equivale a coluna C
  $menor_valor = 999999;
  if($valor_coluna_a < $menor_valor){
     $menor_valor = $valor_coluna_a;
  }
  else if($valor_coluna_b < $menor_valor){
     $menor_valor = $valor_coluna_b;
  }
  else if($valor_coluna_c < $menor_valor){
       $menor_valor = $valor_coluna_c;
  }

In this attempt above, what I imagined that I could get the lowest value in a correct way does not occur, what the error of this logic?

1 answer

0

Hello from what I saw in your logic the problem and that you are using Else if as it comes to 3 comparisons should be 3 if for example


  $valor_coluna_a = 50;
  $valor_coluna_b = 48;
  $valor_coluna_c = 52;

  $menor_valor = 999999;
  if($valor_coluna_a < $menor_valor){ 
      // Verdade - Entrou Aqui e Atualizou a Variavel
     $menor_valor = $valor_coluna_a;
     // $menor_valor = 50
  } else if($valor_coluna_b < $menor_valor){ 
      // Verdade - Porém não entra aqui como se trata de um if else ele
      // ja entrou na primeira condição portanto não executa mais o resto.
     $menor_valor = $valor_coluna_b;
  } else if($valor_coluna_c < $menor_valor){
       $menor_valor = $valor_coluna_c;
  }

best way to solve this would be by making 3 if comparisons shall be made accordingly.


  $valor_coluna_a = 50;
  $valor_coluna_b = 48;
  $valor_coluna_c = 52;

  $menor_valor = 999999;
  
  if($valor_coluna_a < $menor_valor){ 
     $menor_valor = $valor_coluna_a;
     // Menor Valor = 50;
  } 

  if($valor_coluna_b < $menor_valor){ 
     $menor_valor = $valor_coluna_b;
     // Menor Valor = 48;
  } 
  
  if($valor_coluna_c < $menor_valor){
       $menor_valor = $valor_coluna_c;
  }

  // Menor Valor = 48

hope I’ve helped.

Browser other questions tagged

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