smc Posted March 10, 2007 Share Posted March 10, 2007 Hello all, I've got a rather nooby question for you all but I never really mastered arrays. Basically I have a very long array of integers. What I want to do is have MySQL fetch a number and then search through that array stoping at a number which is bigger than the number fetched then reporting the number it is bigger than and the number it is smaller than. Any help would be much appreciated! PS: Also, would there be a way to do this for every value in an array. IE have an array of column names and then for every column name get the value and pan through the array. PSS: In addition after all that is done could it report the position in the array of the last value it was bigger than? Thanks!!!! Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 10, 2007 Share Posted March 10, 2007 Look in to in_array function perhaps. Quote Link to comment Share on other sites More sharing options...
jscix Posted March 10, 2007 Share Posted March 10, 2007 Best I can do is give you some good reading. Hope it helps some. http://us3.php.net/array http://us2.php.net/count http://us2.php.net/while http://us3.php.net/preg_match http://us2.php.net/list Reading all of these, should give you a good idea of what to do, I'm still learning myself so sorry I couldn't be more help. Quote Link to comment Share on other sites More sharing options...
per1os Posted March 10, 2007 Share Posted March 10, 2007 How I came to this was looking at arrays in php.net and found the function array_search looked at it and it told me what it did. php.net solves all your answers! http://us3.php.net/manual/en/function.array-search.php <?php // Part 1: $array = array(2,1,3,4,6,5,7,8,9,10); sort($array); $numToFind = 5; $index = array_search($numToFind, $array); $lower = $array[$index - 1]; $higher = $array[$index + 1]; print "The index of " . $numToFind . " is " . $index . " and the lower Num is " . $lower . " and the higher num is " . $higher . "!"; ?> I will look into the second question now once I figure out what you are trying to say. --FrosT Quote Link to comment Share on other sites More sharing options...
Barand Posted March 10, 2007 Share Posted March 10, 2007 What if the number you fetch from the db is equal to one of the numbers in your array? 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.