How to query the value of a range?-php Tutorial

Source: Internet
Author: User
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 ~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.