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 8701185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:20:10+00:00 2026-06-13T02:20:10+00:00

I’m trying to find the highest key of a PHP array, based on another

  • 0

I’m trying to find the highest key of a PHP array, based on another value.

$values = array(0,0,50,100,200,400,800);

If I let’s say I have the value of 125, it should return 3. And it should return 5 if I have any value between 400 and 799.

$output = -1;
$input = 436;
$length = count($values);
for($i=1;$i<=$length;$i++){
    if($values[$i]<=$input) { $output++; }
}
// Returns 5

I can loop through the array, but there is 100 values, and that can slow the page down a lot because it is used ~20 times per page load per user. Is there a special function I’m missing? Or am I going to have to foreach the array?

The array is always in order and never changes.

  • 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-06-13T02:20:11+00:00Added an answer on June 13, 2026 at 2:20 am

    You may be able to do this using array_flip().

    [ghoti@pc ~]$ php -r '$n=150; $a=array(0,0,50,100,200,400,800); $a[]=$n; sort($a); $f=array_flip($a); print $f[$n]-1 . "\n";'
    3
    [ghoti@pc ~]$ php -r '$n=401; $a=array(0,0,50,100,200,400,800); $a[]=$n; sort($a); $f=array_flip($a); print $f[$n]-1 . "\n";'
    5
    [ghoti@pc ~]$ php -r '$n=200; $a=array(0,0,50,100,200,400,800); $a[]=$n; sort($a); $f=array_flip($a); print $f[$n]-1 . "\n";'
    4
    [ghoti@pc ~]$ php -r '$n=0; $a=array(0,0,50,100,200,400,800); $a[]=$n; sort($a); $f=array_flip($a); print $f[$n]-1 . "\n";'
    1
    

    How does this work?

    Let’s break it down.

    • $n=0; Our number.
    • $a=array(0,0,50,100,200,400,800); Our array.
    • $a[]=$n; We add our number to the array…
    • sort($a); Sort the thing to place our number in the correct position…
    • $f=array_flip($a); Then we exchange values with indices,
    • print $f[$n]-1 And return the value (formerly the index) of the item before the one we added.

    There may be confusion if $a contains multiple entries of the same number, for example 0 in your example. If this happens, you may need to adjust your adjustment (-1 in my example) because an index can only appear once in an array. (For example, if 0 appeared three times, you’d need to adjust your result with -2. This adjustment could be added with extra code, but I’m sure you can figure that part out yourself. 🙂

    This avoids the loop, but be careful if $n=0.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.