Implementing a simple lookup array

It’s a matter of what are you going to do with your lookup.

  • If it’s just a lookup of key -> value pairs – array is a way to go
  • If you want to perform different actions based on the key – it’s actually a good use case for Strategy pattern – no case or array that way at all.

So, case option is inferior in most cases, as it is less scalable and not able to change in run time.

To simulate the default case, use something like

$result = in_array($key, $lookup) ? $lookup[$key] : $default;

Leave a Comment