Efficient Array Matching

Joined: 11/28/2008

Hello.
I currently have an array of ~2500 items, each one of length about 10characters, just A-Za-z0-9, no extra characters.

Here's the pseudo-code of what I want:

CODE
if($inputstring matches any item in array)
    { redirect to "/path-to/$inputstring" }
   else
     { redirect to "/different-path-to/$inputstring" }

I want to do this efficiently, so that it doesn't take up lots of CPU.

I don't mind putting them in an database if that would be better, at present the array is already sorted alpha-numerically.

I'm using php.
Thanks very much.

Joined: 11/28/2008
Looping through an array of

Looping through an array of 2500 items would defiantly use some CPU, but that doesn't mean that it isn't the most efficient way to go. Could you provide just a top level overview of what the elements in the array actually are? Their purpose and origin would determine if a database is the way to go or not.

Joined: 11/28/2008
"14to16","40thpoetry","
QUOTE
"14to16","40thpoetry","850mhz","academicoffice","accommodation","ancillary",
"arthistory","av","accessibility","accidents","accounting","actionresearch_la",
"activia","adairrichards","adamkelly","admissions","adr","adsfab","advantage",
"aerial","agaricusgenome","aiesec","ajlawrance","alisonrodger","alumni",
"am101","am102","am202","am203",

They've essentially search terms which have "perfect" results pages already created.
If people search for these things, there is a specific page they want to go to.

Joined: 11/28/2008
There is an Apache httpd

There is an Apache httpd method which involves a lookup text file or script:
http://httpd.apache.org/docs/2.2/mod/mod_r...html#rewritemap
http://httpd.apache.org/docs/2.2/rewrite/r...ernal-rewriting
But you need root access to the server.

If you want to cut down on CPU usage, you can do some basic indexing: if the first letter is "a", only look at the "a" array, (etc for each letter/number). But that assumes an exact match which is far more efficient than a partial match. If you want partial matching, I think a database lookup (using LIKE on an indexed table) would be the way to go. But an index again will only work best for something like "abcd%" and not "%abcd%". It really depends on what sort of efficiency you need. If you can use some indexing it will make a huge difference, database or not.

Paul Davey
Whitford Church
"Everyone who calls on the name of the Lord will be saved." Romans 10:13
"For all have sinned and fall short of the glory of God, and are justified

Joined: 11/28/2008
I'm only looking for exact

I'm only looking for exact matches, so I'll index them by first letter and see how that goes.
That, however, is work for another day. I'll try and get back to you with my results.

skenow
skenow's picture
PHP in_array( is probably

PHP in_array() is probably the quickest way that I know of.