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

  • Home
  • SEARCH
  • 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 6056881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:25:36+00:00 2026-05-23T08:25:36+00:00

I have a large-ish array of around 500 integer values. I have another integer,

  • 0

I have a large-ish array of around 500 integer values. I have another integer, which I need to test against each of the values in the array to see if it meets the condition of being between the array value – 15 and the array value + 15. Other than just iterating through each value in the array and doing the comparison, is there a neater, more efficient way to do this? I’m using PHP 5.2

EDIT: So if i have array(10,…,2500) and $n = 2510, I want to see if $n matches the condition of being between 15 either side of any of the array values. In this case, the condition would be true for the last value, 2500.

I realise 500 isn’t exactly a monstrous array 🙂

  • 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-23T08:25:36+00:00Added an answer on May 23, 2026 at 8:25 am

    The following snippet will return the values between n-15 to n+15 in the array on my run-of-the-mill desktop in less than 0.001 seconds (and that includes generating the array with values to search for):

    $n = 2500;
    print_r(
        array_filter(
            array_map(function($v) {return $v * 10;}, range(1,500)),
            function($v) use ($n) {
                return $v > $n-15 && $v < $n+15;
            }
        )
    );
    

    I understood you are using PHP5.2 and cannot use the exact snippet above due to the Closure and Lambda, but that’s not the point (just use regular functions for the callbacks). The point is, if less than 0 seconds for 500 array values is not good enough in the overall scope of your application, then go ahead and write an algorithm in userland. But if less than 0 seconds is fine, then dont try to outsmart PHP.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an existing large(ish) PHP web app (using Apache and MySQL) which now
I have a large-ish web project which is migrating from classic ASP to ASP.NET
We have a large-ish query here that has several params, and for each one,
I have large log files which contains timestamps every one second.what I need is
I have a large-ish PHP project (hundreds of files) that I've been brought in
I have a large(ish) COM object that works as the back end of my
I've got a large-ish MySQL database which contains weather information. The DB is about
I have a large-ish file (4-5 GB compressed) of small messages that I wish
I am developing a large-ish application in WPF/WCF/NHibernate/etc. and have implemented the MVP pattern
I'm developing a game with AS3 and AIR. I will have a large-ish quantity

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.