Jump to content

Searching through an array


smc

Recommended Posts

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!!!!

Link to comment
Share on other sites

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

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.