Is there a way to use an array as a list of values to match against in a match expression in PHP?
Consider this:
return match ($field) {
'id', 'first_name', 'last_name', 'updated_at', 'created_at' => (string) $item->{$field},
'image' => $item->{$field}->getUrls(),
'balance' => (int) $item->{$field},
default => null
};
Whilst the above works, I would like to format the match statement in a more readable/manageable manner. For instance, I use the above expression on my data transformers for presenting data in my APIs; as such, I can find myself in situations where there are many fields returned in one format, and it would be significantly more readable to present it in the following manner (or something similar):
$string_columns = ['id', 'first_name', 'last_name', 'updated_at', 'created_at'];
return match ($field) {
$string_columns => (string) $item->{$field},
'image' => $item->{$field}->getUrls(),
'balance' => (int) $item->{$field},
default => null
};