Re: RFC Draft: Comprehensions
| From: | Robert Hickman | Date: | Wed, 20 Mar 2019 00:09:54 +0000 |
| Subject: | Re: RFC Draft: Comprehensions | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-104812@lists.php.net to get a copy of this message | ||
> > Why not apply the same approach to PHP? There is iterator_to_array() to
> > convert a generator to an array, so we may not need both syntaxes.
> > However, I think using [] for something that is *not an array* is
> > counter-intuitive.
>
> No, I would definitely be for []-syntax producing an array. As I said,
> that's in my experience what people usually want.
>
In python comprehensions on [] makes a list and comprehensions in {}
make a dictionary (list and dict comprehensions). As PHP only has one
'array' type using [] makes sense. Along that train of thought, should
comprehensions also be possible in the old array() syntax?
Does the proposal include comprehensions which build associative
arrays? In python you can do {a['key'] : a for a in lst}, where lst is
a list of dicts. I often use this to create a dictionary whose keys
index a sub-element of a nested dictionary, as my example shows.