Re: [PHP-DEV] Calling array_merge / array_merge_recursive without arguments

This is only part of a thread. view whole thread
  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