Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6917145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:41:36+00:00 2026-05-27T09:41:36+00:00

I have a 2 dimensional arrays in php containing the Ranges. for example: From………To

  • 0

I have a 2 dimensional arrays in php containing the Ranges. for example:

From.........To
---------------
125..........3957
4000.........5500
5217628......52198281
52272128.....52273151
523030528....523229183  

and so on

and it is a very long list. now I want to see if a number given by user is in range.
for example numbers 130, 4200, 52272933 are in my range but numbers 1, 5600 are not.

of course I can count all indexes and see if my number is bigger than first and smaller than second item. but is there a faster algorithm or a more efficient way of doing it using php function?

added later

It is sorted. it is actually numbers created with ip2long() showing all IPs of a country.
I just wrote a code for it:

$ips[1] = array (2,20,100);
$ips[2] = array (10,30,200);
$n=11;// input ip
$count = count($ips);
for ($i = 0; $i <= $count; $i++) {
    if ($n>=$ips[1][$i]){
        if  ($n<=$ips[2][$i]){
            echo "$i found";
            break;
        }
    }else if($n<$ips[1][$i]){echo "not found";break;}
}

in this situation numbers 2,8,22,and 200 are in range. but not numbers 1,11,300

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:41:37+00:00Added an answer on May 27, 2026 at 9:41 am

    Put the ranges in a flat array, sorted from lower to higher, like this:

    a[0] = 125
    a[1] = 3957
    a[2] = 4000
    a[3] = 5500
    a[4] = 5217628
    a[5] = 52198281
    a[6] = 52272128
    a[7] = 52273151
    a[8] = 523030528
    a[9] = 523229183
    

    Then do a binary search to determine at what index of this array the number in question should be inserted. If the insertion index is even then the number is not in any sub-range. If the insertion index is odd, then the number falls inside one of the ranges.

    Examples:

    n = 20  inserts at index 0 ==> not in a range
    n = 126 inserts at index 1 ==> within a range
    n = 523030529 inserts at index 9 ==> within a range
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on this a while. I see multi-dimensional arrays in php
I have several situations where I need to pass multi-dimensional PHP arrays into Javascript/jQuery.
I have a multi dimensional array in PHP. $f = array('one' => array(*doesntmatter*), two
If I have a multi dimensional array in PHP like so... [0] => Array
I have an multi-dimensional array that I want to send to a PHP script
Possible Duplicate: php multi-dimensional array remove duplicate I have an array like this: $a
I have a multi dimensional array. The only actual values (other than other arrays)
I am so used to work in PHP with multi-dimensional arrays, where I can
I have to send a two-dimensional JavaScript Array to a PHP page. Indeed, I'm
I have an single-dimensional array of PHP objects. Each object has two attributes, one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.