note 127904 added to language.enumerations.static-methods
| From: | AaronSaray at main dot php dot net | Date: | Tue, 08 Nov 2022 22:29:12 +0000 |
| Subject: | note 127904 added to language.enumerations.static-methods | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219859@lists.php.net to get a copy of this message | ||
Need to retrieve all the names and values immediately from a backed enum (for something like a
select box) and you don't want to loop over
Enum::cases()? Try this:
<?php
enum Suit: string
{
case Hearts = 'H';
case Diamonds = 'D';
case Clubs = 'C';
case Spades = 'S';
public static function forSelect(): array
{
return array_combine(
array_column(self::cases(), 'value'),
array_column(self::cases(), 'name')
);
}
}
Suit::forSelect();
?>
Put forSelect() in a trait and use it in any enum you have that needs this
functionality.
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.159.161 (proxied: 65.31.124.210)
----
Manual Page -- https://php.net/manual/en/language.enumerations.static-methods.php
Edit -- https://main.php.net/note/edit/127904
Del: integrated -- https://main.php.net/note/delete/127904/integrated
Del: useless -- https://main.php.net/note/delete/127904/useless
Del: bad code -- https://main.php.net/note/delete/127904/bad+code
Del: spam -- https://main.php.net/note/delete/127904/spam
Del: non-english -- https://main.php.net/note/delete/127904/non-english
Del: in docs -- https://main.php.net/note/delete/127904/in+docs
Del: other reasons-- https://main.php.net/note/delete/127904
Reject -- https://main.php.net/note/reject/127904
Search -- https://main.php.net/manage/user-notes.php