tippy_102 Posted January 17, 2009 Share Posted January 17, 2009 I have an array of prices and I want to return an array that contains all prices between two values. example: $test = array('2.50','2.75','3.25','3.70','8.45','9.20','9.45'); Say I wanted to grab all prices between 3.00 and 9.00 Is there a way to do this without walking through each value and testing for greater and less than? Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 17, 2009 Share Posted January 17, 2009 Well, I can think of one way where you would only have to test "some" of the values. Don't know how efficient this would be. function includedValues($array, $min, $max) { sort($array, SORT_NUMERIC); //Remove values less than the min while($array[0]<$min) { array_shift($array); } //Remove values greater than the max while($array[count($array)-1]>$max) { array_pop($array); } return array; } Quote Link to comment Share on other sites More sharing options...
tippy_102 Posted January 17, 2009 Author Share Posted January 17, 2009 That's exactly what I needed. Thanks mjdamato! 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.