Reputation: 26321
I often find myself needing a simple PHP lookup array/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];
Upvotes: 5
Views: 14381
Reputation: 48031
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.
A modern equivalent of the above snippet can be realized with match()
if you don't need to leverage the lookup array for any subsequent processes. Demo
foreach (range(0, 5) as $test) {
printf(
"%s %s\n",
$test,
match($test) {
1 => 'Good',
2 => 'Bad',
3 => 'Ugly',
default => null
}
);
}
Upvotes: 1
Reputation: 396
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
Upvotes: 3
Reputation: 32272
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;
Upvotes: 3
Reputation: 1263
Definitely the latter, particularly if you have a lot of data...
Upvotes: -1
Reputation: 26951
It's a matter of what are you going to do with your lookup.
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;
Upvotes: 7