original array. Our list of employees has two people with the same name, so we'll have to
build a list of matches. Refer to the code in Listing 15.9. We won't bother sorting the
list. It won't help, because we will be visiting every element of the array. As we visit each
element, we create a new array. The index of this array is the name of the employee. Each
element of the index will be an array of indices in the employee array. Once the index
is created, finding an employee is a single statement. If the name is found in the array, we
can retrieve the index values for the employee array.
This example is not very realistic because we're only making one search, and we're
building the index with each request. The index needs to be built only once, as long as the
employee array doesn't change. You could save the array to a file, perhaps using PHP
serialization functionality, and then load it when needed. I wrote similar code for the
FreeTrade project that indexes keywords that appear in pages of a Web site.
Listing 15.9 Indexing