Re: Calling array_merge / array_merge_recursive without arguments
| From: | Nikita Popov | Date: | Thu, 23 May 2019 19:18:55 +0000 |
| Subject: | Re: Calling array_merge / array_merge_recursive without arguments | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-105782@lists.php.net to get a copy of this message | ||
On Thu, May 23, 2019 at 9:02 PM Dik Takken <d.takken@xs4all.nl> wrote:
> On 21-05-19 10:29, Nikita Popov wrote:
> > This one makes sense in theory, because array_map(null, ...$arrays) is a
> > way to perform a zip operation in PHP. Unfortunately there is an ugly
> > special case if there is only a single array, which makes this not
> actually
> > usable in practice, unless you know that there are at least two arrays.
> As
> > such, accepting zero arrays wouldn't be very useful unless we also want
> to
> > fix that issue (PHP 8 maybe?)
>
> Ah, I overlooked that edge case. Indeed, array_map() would require more
> fixing than I anticipated. I guess it is out of scope for the type of
> fix we are discussing.
>
> >> array_diff() & friends
> >
> > array_intersect() & friends
> >
> >
> > These look like good candidates for allowing a single arg.
>
> For array_intersect(), I was actually considering to allow zero
> arguments. The use case I have in mind is to determine the common
> elements in a set of zero or more arrays, like this:
>
> array_intersect(...$arrays)
>
> If that would just return an empty array when $arrays is empty, no
> additional empty check is needed and the above would do what I
> intuitively expect it to do.
>
Common intuition, but not mathematically sound. Assuming we're intersecting
subsets of a certain universe U, then the intersection of no sets is U
itself, not the empty set. (A useful intuition is that intersecting
additional sets can only make the result smaller -- as such intersecting no
sets at all yields the largest possible result.)
Nikita