Re: Fwd: [PHP-DEV] clarify the destructuring list() concept
| From: | Larry Garfield | Date: | Fri, 05 Jan 2024 23:47:54 +0000 |
| Subject: | Re: Fwd: [PHP-DEV] clarify the destructuring list() concept | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-122129@lists.php.net to get a copy of this message | ||
On Fri, Jan 5, 2024, at 5:16 PM, Mönôme Epson wrote:
> ---------- Forwarded message ---------
> I try to follow the procedure of: https://wiki.php.net/rfc/howto£†
> ã~±å!‚’Šó
>
> It seems to me that list() is not clearly specified.
> I wanted to know your opinions on this.
>
>
> For example :
> $key = "a";
> $value = 1;
> $array = [$key => $value];
>
> [$key => $value] = $array;
>
> We all agree that now $key = 'a' and $value = 1
>
> And I wonder why
> What happens in this case :
>
> [$key => $value] = ['b'=>2];
>
> $key = 'b' and $value = 2 or all is null ?
Please don't top-post.
As for what that does:
https://3v4l.org/5ubJU
That's because the right-side can have many keys with their own values, but the left side...
doesn't really do that. The left side only assigns values, not keys.
I'm not sure how it would be useful to decompose like you're describing, since it would
only really make sense with a single array item on the left, which is not a common or useful case.
What you can do, as Rowan noted, is this:
https://3v4l.org/cEmcC
That pulls items out of the array by key, into named variables.
--Larry Garfield