Getting values for an enum?



PHP Snippet 1:

$values = [];
foreach ( Type::cases() as $case ) {
    $values[] = $case->value;
}

PHP Snippet 2:

$values = array_column(Type::cases(), 'value');

PHP Snippet 3:

$enum_objects_as_list = Type::cases();
// [Type::OFFENSIVE, Type::SPAM, Type::IRRELEVANT]

$values_as_list = array_column(Type::cases(), 'value');
// [1, 2, 3]

$names_as_list = array_column(Type::cases(), 'name');
// ['OFFENSIVE', 'SPAM', 'IRRELEVANT']

$name_to_value_lookup = array_column(Type::cases(), 'value', 'name');
// ['OFFENSIVE' => 1, 'SPAM' => 2, 'IRRELEVANT' => 3]

$value_to_name_lookup = array_column(Type::cases(), 'name', 'value');
// [1 => 'OFFENSIVE', 2 => 'SPAM', 3 => 'IRRELEVANT']

$name_to_enum_object_lookup = array_column(Type::cases(), null, 'name');
// ['OFFENSIVE' => Type::OFFENSIVE, 'SPAM' => Type::SPAM, 'IRRELEVANT' => Type::IRRELEVANT]

$value_to_enum_object_lookup = array_column(Type::cases(), null, 'value');
// [1 => Type::OFFENSIVE, 2 => Type::SPAM, 3 => Type::IRRELEVANT]

PHP Snippet 4:

$list = array_map(fn($i) => $i->value, Type::cases());