Reputation: 53597
I have a list of values I have to check my input against it for existence.
What is the fastest way?
This is really out of curiosity on how the internals work, not any stuff about premature optimization etc...
1.
$x=array('v'=>'','c'=>'','w'=>);
..
..
array_key_exists($input,$x);
2.
$x=array('v','c','w');
..
..
in_array($input,$x);
Upvotes: 1
Views: 159
Reputation: 51950
How about isset($x[$input])
which, if suitable for your needs, would generally beat both of those presented.
Of the two methods in the question, array_key_exists
has less work to do than in_array
so if you had to choose between only those two then array_key_exists
would be it.
Aside: Do you have any specific questions about "the internals"?
Upvotes: 2
Reputation: 2049
in my experience, array_key_exists is faster 99% of the time, especially as the array size grows.
that being said, isset is even faster, as it does a hash lookup vs an array value search, though isset will return false on blank values, as shown in your example array.
Upvotes: 0