So I was about to write something like following code
switch ($mode) {
case 'all':
foreach ($sortInfo as $info) $filter[] = array_merge([$info->maincat], $info->subcats);
break;
case 'sub':
foreach ($sortInfo as $info) $filter[] = $info->subcats;
break;
default:
foreach ($sortInfo as $info) $filter[] = [$info->maincat];
break;
}
Then I told myself "hey, wouldn't it be more optimized if I wrapped the whole switch inside the for loop?"
foreach ($sortInfo as $info) {
switch ($mode) {
case 'all':
$filter[] = array_merge([$info->maincat], $info->subcats);
break;
case 'sub':
$filter[] = $info->subcats;
break;
default:
$filter[] = [$info->maincat];
break;
}
}
But while it does technically save a bit (get it?) of filesize, the loop would confirm the same information in the switch statement for each iteration of the loop.
I figured there is no objective way to determine which is fastest because it depends on the length of $sortinfo
, but since it's the first time I come across this dilemma, I'd like to know if there's a preferred method, or what's your take on it.