minimizing the number of array lookups
Posted: Sun Sep 05, 2004 4:39 am
Hello
Assume I have the following code:
This function uses 2 array lookup to ensure that a certain key is indeed defined in an array (in case the key is indeed defined).
Is there a way to reduce the number of lookups to 1?
Here is what I would like to do:
Perform a single lookup that does the check but also gives you an index. If check if successful the second access can be done via the index to be immediate instead of going through hash table
regards
Jason
Assume I have the following code:
Code: Select all
function arrayLookup($array, $key) {
if (!array_key_exists($key, $array)) {
return $phrase;
}
return $array[$key];
}Is there a way to reduce the number of lookups to 1?
Here is what I would like to do:
Perform a single lookup that does the check but also gives you an index. If check if successful the second access can be done via the index to be immediate instead of going through hash table
regards
Jason