How can PHP quickly determine what range a number belongs to?

Source: Internet
Author: User
Tags ranges
The demand is like this ...

if ( $foo > 0 && $foo < 100 ) $bar = 1;elseif ( $foo > 99 && $foo < 212 ) $bar = 2;elseif ( $foo > 211 && $foo < 324 ) $bar = 3;elseif ( $foo > 323 && $foo < 382 ) $bar = 4;elseif ( $foo > 381 && $foo < 465 ) $bar = 5;elseif ( $foo > 464 && $foo < 552 ) $bar = 6;# ...

There are thousands of such rules ... So the full screen is $foo > __ && $foo < __ $bar = __ ...

Efficiency and aesthetics are problematic ...

The value of the critical point is near random ... I can't think of a formula that can be extrapolated $bar by $foo ...

The current idea is to divide all the rules into two ... This can improve efficiency ... But aesthetics is still a problem ...

If it is sealed in a function ... It looks really beautiful ... But efficiency is worse than this ...

Is there a more perfect solution for both?

Additional... I wrote a scheme with array sorting ...

Efficiency and self-realization functions are almost ... Still less than two points ...

Reply content:

The demand is like this ...

if ( $foo > 0 && $foo < 100 ) $bar = 1;elseif ( $foo > 99 && $foo < 212 ) $bar = 2;elseif ( $foo > 211 && $foo < 324 ) $bar = 3;elseif ( $foo > 323 && $foo < 382 ) $bar = 4;elseif ( $foo > 381 && $foo < 465 ) $bar = 5;elseif ( $foo > 464 && $foo < 552 ) $bar = 6;# ...

There are thousands of such rules ... So the full screen is $foo > __ && $foo < __ $bar = __ ...

Efficiency and aesthetics are problematic ...

The value of the critical point is near random ... I can't think of a formula that can be extrapolated $bar by $foo ...

The current idea is to divide all the rules into two ... This can improve efficiency ... But aesthetics is still a problem ...

If it is sealed in a function ... It looks really beautiful ... But efficiency is worse than this ...

Is there a more perfect solution for both?

Additional... I wrote a scheme with array sorting ...

Efficiency and self-realization functions are almost ... Still less than two points ...

Assuming that your range is contiguous (in fact discontinuous and easy to implement), there is no overlap (this is OK), then by sorting the starting position of the range, it can be easily achieved by two points.

$ranges = array(1, 100, 212, 324, 382, 465, 552);

And then what you have to do is find a satisfaction in the ranges with a binary search a[i] a[i] <= t && t < a[i+1] .

P.S. Spit your range, I see a little schizophrenic.

  • 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.