Re: [PHP-DEV] Calling array_merge / array_merge_recursive withoutarguments

This is only part of a thread. view whole thread
  105750
May 18, 2019 19:37 d.takken@xs4all.nl (Dik Takken)
On 17-05-19 22:52, G. P. B. wrote:
> > So as a side note aren't there maybe some other array functions which > behave like this? >
Good point. I did a quick scan of the source code and came up with a list of possible candidates for a similar treatment: array_map() array_diff() & friends array_intersect() & friends compact() While I would not expect these functions to be used a lot with spread operators in the same way array_push() or array_merge() are used, fixing them in the same way may still be good for the sake of consistency. Any other functions I overlooked?
  105754
May 19, 2019 19:13 george.banyard@gmail.com ("G. P. B.")
On Sat, 18 May 2019 at 21:37, Dik Takken takken@xs4all.nl> wrote:

> On 17-05-19 22:52, G. P. B. wrote: > > > > So as a side note aren't there maybe some other array functions which > > behave like this? > > > > Good point. I did a quick scan of the source code and came up with a > list of possible candidates for a similar treatment: > > array_map() > array_diff() & friends > array_intersect() & friends > compact() > > While I would not expect these functions to be used a lot with spread > operators in the same way array_push() or array_merge() are used, fixing > them in the same way may still be good for the sake of consistency. > > Any other functions I overlooked? >
Doesn't seem there are other relevant array functions which could benefit from this after having a glance at it. Best regards George P. Banyard
  105770
May 21, 2019 08:29 nikita.ppv@gmail.com (Nikita Popov)
On Sat, May 18, 2019 at 9:37 PM Dik Takken takken@xs4all.nl> wrote:

> On 17-05-19 22:52, G. P. B. wrote: > > > > So as a side note aren't there maybe some other array functions which > > behave like this? > > > > Good point. I did a quick scan of the source code and came up with a > list of possible candidates for a similar treatment: > > array_map() >
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?)
> array_diff() & friends
array_intersect() & friends These look like good candidates for allowing a single arg. Nikita
  105772
May 21, 2019 10:54 george.banyard@gmail.com ("G. P. B.")
On Tue, 21 May 2019 at 10:29, Nikita Popov ppv@gmail.com> wrote:

> On Sat, May 18, 2019 at 9:37 PM Dik Takken takken@xs4all.nl> wrote: > >> On 17-05-19 22:52, G. P. B. wrote: >> > >> > So as a side note aren't there maybe some other array functions which >> > behave like this? >> > >> >> Good point. I did a quick scan of the source code and came up with a >> list of possible candidates for a similar treatment: >> >> array_map() >> > > 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?) > > Nikita >
I'm not sure I totally understand the edge case, moreover this isn't currently documented. Mind sharing an example or explaining a bit more in detail such that I can update the documentation? Best regards George P. Banyard
  105773
May 21, 2019 11:40 cmbecker69@gmx.de ("Christoph M. Becker")
On 21.05.2019 at 12:54, G. P. B. wrote:

> On Tue, 21 May 2019 at 10:29, Nikita Popov ppv@gmail.com> wrote: > >> On Sat, May 18, 2019 at 9:37 PM Dik Takken takken@xs4all.nl> wrote: >> >>> Good point. I did a quick scan of the source code and came up with a >>> list of possible candidates for a similar treatment: >>> >>> array_map() >> >> 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?) > > I'm not sure I totally understand the edge case, moreover this isn't > currently documented. > Mind sharing an example or explaining a bit more in detail such that I can > update the documentation?
If you pass only a single array to array_map(NULL, …), you don't get an array of arrays, but a single flat array, see <https://3v4l.org/sqtqS>. -- Christoph M. Becker
  105774
May 21, 2019 13:17 george.banyard@gmail.com ("G. P. B.")
On Tue, 21 May 2019 at 13:40, Christoph M. Becker <cmbecker69@gmx.de> wrote:

> On 21.05.2019 at 12:54, G. P. B. wrote: > > > On Tue, 21 May 2019 at 10:29, Nikita Popov ppv@gmail.com> wrote: > > > >> On Sat, May 18, 2019 at 9:37 PM Dik Takken takken@xs4all.nl> wrote: > >> > >>> Good point. I did a quick scan of the source code and came up with a > >>> list of possible candidates for a similar treatment: > >>> > >>> array_map() > >> > >> 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?) > > > > I'm not sure I totally understand the edge case, moreover this isn't > > currently documented. > > Mind sharing an example or explaining a bit more in detail such that I > can > > update the documentation? > > If you pass only a single array to array_map(NULL, …), you don't get an > array of arrays, but a single flat array, see <https://3v4l.org/sqtqS>. > > -- > Christoph M. Becker >
Thanks Christoph for the example didn't even really know that you could use null as a callback. So I suppose the "correct" behaviour wanted for array_map with NULL as a callback is to always return an array of arrays instead of a flat one when there is only one, correct? I think I'll add a small note to the doc and an example showing the difference in behaviour as it seems pretty berried in the examples (the 4th one to be precise atm). Best regards George P. Banyard
  105775
May 21, 2019 16:31 george.banyard@gmail.com ("G. P. B.")
On Tue, 21 May 2019 at 14:17, G. P. B. banyard@gmail.com> wrote:

> I think I'll add a small note to the doc and an example showing the > difference in behaviour as it seems pretty berried in the examples (the 4th > one to be precise atm). >
Added a note in the doc: http://svn.php.net/viewvc?view=revision&revision=347462 Best regards George P. Banyard
  105781
May 23, 2019 19:02 d.takken@xs4all.nl (Dik Takken)
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.
  105782
May 23, 2019 19:18 nikita.ppv@gmail.com (Nikita Popov)
On Thu, May 23, 2019 at 9:02 PM Dik Takken 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
  105816
June 1, 2019 11:49 d.takken@xs4all.nl (Dik Takken)
On 23-05-19 21:18, Nikita Popov wrote:
> On Thu, May 23, 2019 at 9:02 PM Dik Takken takken@xs4all.nl> wrote: > >> 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 >
You are completely right, thanks for pointing that out. Regards, Dik Takken
  105815
June 1, 2019 11:44 d.takken@xs4all.nl (Dik Takken)
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?)
How about moving this zip functionality out of array_map() into a newly introduced zip() method? That sounds like something that would be feasible for me to do, given my limited knowledge of PHP internals. What would be the proper path, in terms of PHP releases, for making this change? Maybe something like: 1. Add zip() and deprecate null callbacks in array_map() in PHP 7.4 2. Remove null callbacks support in array_map() in PHP 8 3. Allow passing zero arrays to array_map() in PHP 8 Do these changes require an RFC? Regards, Dik Takken