I have a list of 300 numbers, they are not ordered. I would like to find the most efficient way to determine if a number is in that list.
The simple answer is in_array(). This of course has a Big O of O(n).
But since I have complete control over this list, I believed I can make it faster.
So I made the list an associative array where key is the number I am looking for which an isset() will yield me O(1).
$myArray = array('434342'=>true, '345235'=>true, '562211'=>true, '3333245'=>true, '99087782'=>true);
Is this the best way? Since the array size is small the hit of O(n) where n=300 its not worth the extra effort.
isset()is not noticeably more efficient than just using a numerical array andin_array(). It definitely, in my opinion, is not worth the decrease of legibility to another programmer (I would scratch my head if I saw an array like the one in your example). If you really want to know, benchmark it.