Jump to content

Maximum array element that exceeds variable value


ThisisForReal

Recommended Posts

I've never taken a logic class, or a CS class. This is likely very easy to anyone with any background in either topic.

 

I have an array, $_SESSION['thresholds'], where I've given the array keys values such as "thresholdA", "thresholdB", "thresholdC", "thresholdD", etc. with corresponding elements that increase in value (5,15,50,75,etc.). They array is currently mapped in ascending order.

 

I have a variable, $_SESSION['compare'], that I want to compare against all the elements in the $_SESSION['thresholds'] and, ultimately, I want to be able to echo/print the key of the largest array element that is less than the value of $_SESSION['compare']. Using the above values, if $_SESSION['compare'] == 21, then I would love to echo/print "thresholdB".

 

What's the best method to accomplish this? Array_Walk? A switch statement? I first tried while() and was trying to use the pointer in the array, but I found that if I used next() to see if the next array element was larger, the actual use of next() within the while() statement caused the pointer to advance anyways. That seemed like the wrong path to take.

 

The switch statement I've tried is failing, and I don't know how to use a comparison within an array_walk when I want to break out once the largest value is determined.

 

This seems like such a basic function of array and variables but I'm struggling with this. Any advice would be much appreciated.

 

Here's some of my tests that failed:

reset($_SESSION['thresholds']);
while( (current($_SESSION['compare']) < $_SESSION['thresholds'])
and (key($_SESSION['thresholds']) <> 'thresholdMAX'))	 next($_SESSION['compare']);

 

That final next() statement advances me one step too far. Should I use this and then backup one step? That might create problems of its own.

 

Next I tried switch:

switch ($_SESSION['compare']) {
case ($_SESSION['compare'] >= $_SESSION['thresholds']['thresholdMAX']):
 $output = key($_SESSION['thresholds']['thresholdMAX']);
 break;
case ($_SESSION['compare'] >= $_SESSION['thresholds']['thresholdD']):
 $output = key($_SESSION['thresholds']['thresholdD']);
 break;

 

But that wasn't working and seems like the wrong way to go about this. Can anyone point me in the right direction? Thanks so much in advance!

Edited by ThisisForReal
Link to comment
Share on other sites

A binary search would generally be the most efficient, but if you don't already know it then I wouldn't suggest using it. Plus the fact that you have string keys makes it a bit slower and more complicated.

 

Loop through the thresholds array and a] if the value is smaller then then stick the key in some specific variable or b] if the value is not smaller then break out of the loop. After the loop you'll have the correct key and, by looking it up in the array, the largest value.

Edited by requinix
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.