Re: Check input against list
| From: | Chris Adams | Date: | Wed, 02 Aug 2000 02:52:37 +0000 |
| Subject: | Re: Check input against list | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-9614@lists.php.net to get a copy of this message | ||
> > > Is there some way to check that an input string is one of N
possibilities,
> > > where N is a large-ish number?
>
> I do something similar in one of my forms/scripts.
>
>
> > $States = array("CA", "AZ", "HI");
> >
> > if (isset($States[$UserEnteredState])) echo "Yes, we have service
there!";
> >
> > in_array() can be used very similarly.
>
> Now, THAT'S an interesting idea.
>
> In my scripts, I'm doing a lookup on a text file to find some information
> related to the ISO codes. I store the codes and related info in a CSV
(comma
> separated values) file. Then, I loop through the file with fgetcsv(). If
a
> match happens, I store the related info and break out of the loop. So, if
in
> the lookup doesn't find a match, I know the code is invalid.
The big advantage to something like the above is speed. Numeric indexed
would be absurdly fast but even a hash-lookup is only a bit longer; in
either case it should be much faster than even opening a file (and when the
Zend cache is released it should become even faster).
Of course, if you have a large number of items or need a better way of
managing them it would be better to use something like MySQL. If you can
make the lookup value an indexed key, a simple SELECT COUNT(*) FROM Table
WHERE key='foobar' will take an extremely short period of time and it's
trivial to modify it to retrieve a couple other data items you find
interesting. The big win is indexing, since you avoid the overhead of
parsing every line.
Chris