# Calling array_merge / array_merge_recursive without arguments

- Thread: https://externals.io/message/105736
- Original archive: https://news-web.php.net/php.internals/105736
- Started by Dik Takken on 2019-05-17T19:52:56+00:00
- Messages: 14
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #105736 — Dik Takken — 2019-05-17T19:52:56+00:00

URL: https://externals.io/message/105736#105736

Hello,

I just created a pull request that changes the array_merge /
array_merge_recursive functions such that they allow being called
without arguments:

https://github.com/php/php-src/pull/4175

This is my first attempt to contribute, feedback is highly appreciated.

please advise on how to proceed.

Regards,
Dik Takken

---

## #105738 — G. P. B. — 2019-05-17T20:52:14+00:00

URL: https://externals.io/message/105736#105738

> Hello,
>
> I just created a pull request that changes the array_merge /
> array_merge_recursive functions such that they allow being called
> without arguments:
>
> https://github.com/php/php-src/pull/4175
>
> This is my first attempt to contribute, feedback is highly appreciated.
>
> please advise on how to proceed.
>
> Regards,
> Dik Takken
>

Good catch, I do like the idea it is a bit in the same spirit in how as of
PHP 7.3 array_unshift and array_push can be called without arguments now.

So as a side note aren't there maybe some other array functions which
behave like this?

Best Regards

George P. Banyard

---

## #105739 — Levi Morrison — 2019-05-17T21:12:51+00:00

URL: https://externals.io/message/105736#105739

> I just created a pull request that changes the array_merge /
> array_merge_recursive functions such that they allow being called
> without arguments:
>
> https://github.com/php/php-src/pull/4175

The change seems simple, and the idea is welcome. For me, the only
question is whether to risk merging it in 7.4 or to target 8.0. I
would be happy leaving that decision to maintainers.

---

## #105750 — Dik Takken — 2019-05-18T19:37:28+00:00

URL: https://externals.io/message/105736#105750

> 
> 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 — G. P. B. — 2019-05-19T19:13:25+00:00

URL: https://externals.io/message/105736#105754

> 
> > […]
>
> 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 — Nikita Popov — 2019-05-21T08:29:17+00:00

URL: https://externals.io/message/105736#105770

> 
> > […]
>
> 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 — G. P. B. — 2019-05-21T10:54:49+00:00

URL: https://externals.io/message/105736#105772

> 
>
> > […]
>
> 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 — Christoph M. Becker — 2019-05-21T11:40:25+00:00

URL: https://externals.io/message/105736#105773

> 
>
> > […]
>
> 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 — G. P. B. — 2019-05-21T13:17:16+00:00

URL: https://externals.io/message/105736#105774

> 
>
> > […]
> actually
> > […]
> As
> > […]
> to
> > […]
> can
> > […]
>
> 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 — G. P. B. — 2019-05-21T16:31:25+00:00

URL: https://externals.io/message/105736#105775

> 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 — Dik Takken — 2019-05-23T19:02:26+00:00

URL: https://externals.io/message/105736#105781

> 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_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 — Nikita Popov — 2019-05-23T19:18:55+00:00

URL: https://externals.io/message/105736#105782

> 
> > […]
> actually
> > […]
> As
> > […]
> to
> > […]
>
> 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.
>
> > […]
>
> 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

---

## #105815 — Dik Takken — 2019-06-01T11:44:13+00:00

URL: https://externals.io/message/105736#105815

> 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

---

## #105816 — Dik Takken — 2019-06-01T11:49:25+00:00

URL: https://externals.io/message/105736#105816

> 
> 
> > […]
> 
> 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
