Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

155
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda