ThisisForReal Posted January 10, 2013 Share Posted January 10, 2013 (edited) 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 January 10, 2013 by ThisisForReal Quote Link to comment Share on other sites More sharing options...
requinix Posted January 10, 2013 Share Posted January 10, 2013 (edited) 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 January 10, 2013 by requinix Quote Link to comment Share on other sites More sharing options...
ThisisForReal Posted January 10, 2013 Author Share Posted January 10, 2013 foreach ($_SESSION['threshold'] as $k => $v ) { if ($v > $_SESSION['compare']) { break; } $output = $k; } echo $output; That's the elegant solution I was looking for. Thanks so much! I really appreciate it. Quote Link to comment Share on other sites More sharing options...
requinix Posted January 10, 2013 Share Posted January 10, 2013 Keep in mind: you said "largest array element that is less than the value" so that means if you find something larger than or equal to the value then the loop should stop. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.