Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
Using an Array of values with a Match Expression in PHP

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
};
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Your question inspired me to check if ...$string_columns would work for match statement, but it doesn't.

I don't think there's any way to do it with match, other than tricks.

One trick I thought of was to create a helper function to return the value only if it's in the array:

function any(array $values, $value)
{
    return in_array($value, $values, true) ? $value : null;
}

$string_columns = ['id', 'first_name', 'last_name', 'updated_at', 'created_at'];

return match ($field) {
    any($string_columns, $field) => (string) $item->{$field},
    'image'                      => $item->{$field}->getUrls(),
    'balance'                    => (int) $item->{$field},
    default                      => null
};

A bit verbose, but would work.

I think it's worth to understand match as a language construction that suits only certain scenarios.

over 4 years ago · Santiago Trujillo Report

0

It is not posible. Just add in_array in default value.

return match ($field) {
    'image'         => $item->{$field}->getUrls(),
    'balance'       => (int) $item->{$field},
    default => in_array($value, $string_columns, true) ? 
              (string) $item->{$field} 
              : null;
};

If there will be more arrays, you can chain ternary operator

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!