Re: [RFC] Cast in foreach
| From: | Chris Riley | Date: | Fri, 22 Feb 2019 14:23:56 +0000 |
| Subject: | Re: [RFC] Cast in foreach | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-104506@lists.php.net to get a copy of this message | ||
In this instance, I would expect the cast to come before each variable
eg foreach ($data as ["id" => (int) $id, "name" => (string) $name])
On Thu, Feb 21, 2019, 13:28 Kalle Sommer Nielsen <kalle@php.net> wrote:
> Den tor. 21. feb. 2019 kl. 14.16 skrev Chris Riley <t.carnage@gmail.com>:
> >
> > Hi internals,
> >
> > I'd like to propose opening an rfc to make the following syntax legal:
> >
> > foreach($array as (int) $i) {}
>
> How would this interact with the foreach-list syntax?
>
> $data = [
> ["id" => 1, "name" => 'Tom'],
> ["id" => 2, "name" => 'Fred'],
> ];
>
> foreach ($data as ["id" => $id, "name" => $name]) {
> echo "id: $id, name: $name\n";
> }
>
> Having something like:
> foreach ($data as (int) ["id" => $id, "name" => $name]) {
>
> Would then make both $id and $name integers?
>
> --
> regards,
>
> Kalle Sommer Nielsen
> kalle@php.net
>