$amount = 100; / / 110 br 90200
$arr = [98100100120150160183];
as above: the variable $amount is compared with $arr. If the variable is in the array, it returns the subscript value in the array;
if the variable is not in the array, first detect whether the variable is between which two values (AB), and if so, return the subscript of the AB;
if it is not between the two values, if the variable is less than the first value of the array, return subscript 0;
if the variable is greater than the last value of the array, return the array length by one.
:
$amount = 100; [1,2];
$amount = 110; [2,3];
$amount = 90; [0];
$amount = 200; [6];
the following is the code I wrote to simplify.
$key = []; //
$count = count($arr) - 1;
foreach($arr as $k => $v){
if($amount == $v){
$key[] = $k;
}
}
if(empty($key)){
foreach($arr as $k => $v){
if($amount < $arr[0]){
$key[] = "";
break;
}
}
}
if(empty($key)){
foreach($arr as $k => $v){
if($amount > $arr[$count]){
$key[] = $count;
break;
}
}
}
if(empty($key)){
foreach($arr as $k => $v){
if($amount >= $arr[$k] && $amount < $arr[$k + 1]){
$key[] = $k;
$key[] = $k + 1;
}
}
}
var_dump($key);