Please advise me how to query the value of a range
if($bbb>0&&$bbb<=100){$baifen=100;} elseif($bbb>100&&$bbb<=500){$baifen=500;} elseif($bbb>500&&$bbb<=1000){$baifen=1000;} elseif($bbb>1000&&$bbb<=5000){$baifen=5000;}
To implement such a function
I tried to simplify the writing and found the non-recursive binary method on the Internet, but I did not change it successfully.
$arr[0,100,500,1000,5000];$b[100,500,1000,5000];
Less than 0 is not considered
If $ val = 100, the result is $ B [0].
Is there a big God to teach me how to implement this with a non-recursive binary method?
This is the original code found on the Internet.
Function binarySearch ($ array, $ value) {// start position, which can be used as the function parameter $ low = 0; $ high = count ($ array)-1; while ($ low <= $ high) {$ mid = ceil ($ low + $ high)/2); if ($ array [$ mid] = $ value) return $ mid; if ($ array [$ mid] <$ value) {$ low = $ mid + 1;} if ($ array [$ mid]> $ value) {$ high = $ mid-1 ;}} return-1 ;}
Thanks for not implementing the modification.
Reply to discussion (solution)
Direct condition comparison
if($bbb > 1000) ) $baifen = 5000;elseif($bbb > 500) $baifen = 1000;elseif($bbb > 100) $baifen = 500;elseif($bbb > 0) $baifen = 100;
Use dictionaries
$dict = array(0 => 100, 100 => 500, 500 => 1000, 1000 => 5000);krsort($dict);foreach($$dict as $k=>$v) if($x > $k) break;echo $v;
Direct condition comparison
if($bbb > 1000) ) $baifen = 5000;elseif($bbb > 500) $baifen = 1000;elseif($bbb > 100) $baifen = 500;elseif($bbb > 0) $baifen = 100;
Use dictionaries
$dict = array(0 => 100, 100 => 500, 500 => 1000, 1000 => 5000);krsort($dict);foreach($$dict as $k=>$v) if($x > $k) break;echo $v;
Thank you .. Ask $ v to get the maximum value. for example, $ x = 101 $ v returns 500... I want to return 1 because it is in the second range. I take the first range to get the value, and the second range to get the value ~~ How can I return to the moderator ~~