#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
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

\$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.