Connecting Tech Pros Worldwide Forums | Help | Site Map

Please help wit{h array search technique?

bobmct
Guest
 
Posts: n/a
#1: Sep 29 '05
Gentlemen;

Can someone recommend the best way to search a single dimension array?
I'm currently using a for loop and it doesn't seem to be working
accurately. Here's an example:

$_AV=array(0 => '0100',
1 => '0200',
2 => '0300',
3 => '0400',
4 => '0500'
};

$arg="0225";


for($X=count($_AV);$X>=0;$X--) {
if ($_AV[$X] > $arg) {
$result=$X;
break;
}
}


According to my requirements, the result should be set to 1 which is
the index to the element that is either equal to or less than but
closest to the $arg value.

Is there a better way to accomplish this??

Any/all suggestions appreciated.

Thanks

Faree
Guest
 
Posts: n/a
#2: Sep 29 '05

re: Please help wit{h array search technique?



$diff=50;//Set this to Maximum difference u can allow between numbers.
for($X=count($_AV);$X>=0;$X--)
{
if ($_AV[$X]== $arg)
{
$result=$X;
break;
}

else if(($_AV[$X]<$arg) AND ($arg-$_AV[$X] <=$diff)
{
$result=$X;
break;
}

else if(($_AV[$X]>$arg) AND ($_AV[$X]-$arg<=$diff)
{
$result=$X;
break;
}
else echo "No Matych found wth in this limit";

}

Hope this will work for u.

bobmct
Guest
 
Posts: n/a
#3: Sep 30 '05

re: Please help wit{h array search technique?


On 28 Sep 2005 22:50:22 -0700, "Faree" <th.vijaya@gmail.com> wrote:
[color=blue]
>
> Hope this will work for u.[/color]

Faree;

While I didn't use your code verbatim, with a little tweaking it works
just fine. Thank you for your help.

Bob
Closed Thread