Best method for exact match in array with PHP?

During a job interview one asked me: “how would you search for a word inside a very long array?”. I answered just by using the foreach loop and compare the single content with the searched word. But according to him it would not fit with very big sized arrays, so his suggestion was to flip the array (changing all keys and values) and then to input the word in the key field and see if it’s found. Now, beyond any justifying logic I think it could be interesting to check the reality with some tests.

Let an array being created in three different ways using the following three different functions. The first one creates an array having 1 million different words as values and the last one being literally “word”, which is the string we need to search for. The second one creates an array of 1 million of the elements, after which “word” is always the last element. The third function uses a mixed array, where the first half of all elements is made with different words, and the other half with the same.

Then I measured the time for each kind of array using microtime

The result? I resumed the relative measurements in the following table:

 All elements different Half different, half equal All elements equal time1 (foreach) 0.063 µs 0.059 µs 0.056 µs time2 (flip) 0.119 µs 0.068 µs 0.026 µs (time2/time1)*100 189.38% 114.45% 46.39%

So just in the case most of the array’s values (as strings) are equal to each other it could be convenient to use the flip function. But why should that happen? If we have a words list it’s expected to be different for most of them. After searching for one million of different item the flip method takes around the 189% of the time needed for foreach.

What’s very interesting is what happen if we put numbers instead of words, before the last string value “word” we are searching for. If we build the array with integers using the following function

With such values, the string “word” can be found using the flip function more than 10000 times slower than foreach!

Posterity will judge.