Implementing a simple lookup table with PHP

I often find myself needing a simple PHP lookup table where I don’t want to bother using a database.

For instance, I might have:

1 stands for "Good"
2 stands for "Bad"
3 stands for "Ugly"

Two ways it could be implemented are shown below. Is one more efficient than the other? Is there any other more intuitive ways to implement this?

switch($code)
{
    case 1:$result="Good";break;
    case 2:$result="Bad";break;
    case 3:$result="Ugly";break;
    default:$result=NULL;
}

$array=array(1=>"Good",2=>"Bad",3=>"Ugly");
$result=$array[$code];

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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;

Solution 2

The second example. The main reason being that it’s less code to write in new entries, but it’s also more flexible code, and might be marginally faster. But to implement the default case from the break statement the ‘lookup’ line/function should look like:

$result = (isset($array[$code]) ? $array[$code] : NULL;

Solution 3

appearently you can do this with a much simpler syntax:

$result=[1=>"Good",2=>"Bad",3=>"Ugly"][$code];

the array is initialized with the first pair of [] and specific value is access with the second pair

i have no idea since which version of php this works i have recently encountered this construction on a random tutorial, will update the answer if i come across more details

Solution 4

Definitely the latter, particularly if you have a lot of data…

Solution 5

I recommend a lookup array over a switch block with very few exceptions. Frankly, I have a bias against switch blocks because if they are written with proper coding standards, they bloat the script horribly. Alternatively, you could fight the bloat generated by all those case and break expressions, but then you get a horrifically dense script.

No, I strongly urge a lookup array. It beautifully separates the data assets from the processing. This results in clean, readable, maintainable code. Furthermore, as your application evolves/matures, you can easily port your collection of data to a database table if required.

About efficiency, a lookup technique employing isset() or array_key_exists() (depending on requirements) is very fast because of how php can reference the keys.

Using key-based lookups is going to outperform in_array() everytime.

Here is a snippet for context…

Code: (Demo)

$lookup = [
    null,
    "Good",
    "Bad",
    "Ugly"
];

foreach (range(0, 5) as $test) {
    echo $test , ' : ' , ($lookup[$test] ?? $lookup[0]) , "\n";
}

Output:

0 :         // null
1 : Good
2 : Bad
3 : Ugly
4 :         // null 
5 :         // null 

You can, of course, write out the keys in the lookup array for improved human readability.

Assuming your php version permits it, I recommend the null coalescing operator for brevity.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply