Locate the next "less than" value in an array

Asked

Viewed 1,535 times

3

I have the following array and need to locate the next smallest value that defined in $valor:

$seq = array(500, 490, 430, 370, 350, 240, 100, 90);
$n_seq = count($seq);
$valor = 400; // ASSUME DIFERENTES VALORES
    if ($valor > $seq[0]){
        $encontrado = $seq[0];
        } else {
        for ($i = $n_seq; $seq[$i] > $valor; $i--) {
        $encontrado = $seq[($i+1)];}
        }
echo "Valor atribuído: ".$valor;
echo "<br>";
echo "Valor encontrado: ".$encontrado;

Example: If $valor assumes the value 630, the answer would be 500.

If $valor assumes the value 500, the answer would be 490.

If $valor assumes the value 240, the answer would be 100.

Problem: I don’t know if I can chain if() next to a for(), depending on the values that $valor assumes an error of Undefined offset with Undefined variable.

Someone could help me?

  • You mean, "the highest value in the array that is less than $value"? What do you expect in return if $value is 80? The array elements will always be sorted or this was a particular case of your example?

1 answer

3


Here is a suggestion:

  • Sort the array using the Sort() optionally 1 to do numerical Sort
  • search for the first number that is greater than or equal to $valor (alternatively one could look for the previous value, ie which is the last to be less than $valor)

PHP:

$seq = array(500, 490, 430, 370, 350, 240, 100, 90);
sort($seq, 1);
$n_seq = count($seq);

$valor = 400; // ASSUME DIFERENTES VALORES
$encontrado = '';
for($i = 0; $i < $n_seq; $i++){
    if ($seq[$i] >= $valor){ // ou usar somente ">"
    $encontrado = $i == 0 ? $seq[$i] : $seq[$i - 1]; 
    break;

    } 
}
if ($encontrado == '' && $valor > end($seq)) $encontrado = end($seq);
if ($encontrado == '' && $valor < $seq[0]) $encontrado = $seq[0];

echo "Valor atribuido: ".$valor;
echo "<br>";
echo "Valor encontrado: ".$encontrado;
  • For your solution, I use values outside the range such as 600 don’t get $encontrado or if I use a value that exists in the array as 370 I don’t get the 350, but rather the very 370.

  • @user3486019, for values outside the array I’ve done a code correction, test again. If you don’t want to use equal numbers just change >= for >. I added a comment on the specific line :)

  • OK. But now the code finds the highest subsequent value $valor, I need you to be the smallest.

  • @user3486019, Aha, ok. I corrected for $encontrado = $seq[$i - 1];

  • Almost perfect solution. If $value takes value below the array, example 10, appears Undefined offset. -- $seq[$i - 1] becomes negative value $seq[-1]

  • @user3486019, corigido.

  • Same mistake yet, but now if $valor takes values greater than the array, such as 600, have as an answer 90 and not the 500, as expected.

  • @user3486019, sorry, bug mine, hadn’t tested. I fixed.

  • The only problem is when $valor takes value below the smallest of the array, such as 10 --> Notice: Undefined offset: -1 in

  • @user3486019, in the code I have put now works. I tested with -600 and gave me 90

  • It worked to find the values, but the message appears Notice: Undefined offset: -1 in C:\Program Files\EasyPHP-DevServer-14.1VC11\data\localweb\askbid.php on line 139

  • what line is that? , what code is there?

  • 139 is the line of $encontrado = $seq[$i - 1];

  • I treated the line with an if(). Thank you very much for your help.

Show 9 more comments

Browser other questions tagged

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