Re: Array spread append
| From: | Andreas Hennings | Date: | Wed, 05 Apr 2023 23:39:29 +0000 |
| Subject: | Re: Array spread append | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-119820@lists.php.net to get a copy of this message | ||
> I have the feeling I'm missing something, but how would this compare to
array join
Array join does neither append nor replace if a numeric index already exists.
But you can do [...$arr0, ...$arr1].
https://3v4l.org/CIinR
print json_encode([
[...['a', 'b'], ...['c', 'd']], // [a, b, c, d]
array_merge(['a', 'b'], ['c', 'd']), // [a, b, c, d]
['a', 'b'] + ['c', 'd'], // [a, b]
array_replace(['a', 'b'], ['c', 'd']), // [c, d]
]);
(I wish we could use yaml for these outputs)
> $arr[...] = $arr2;
Using spread in the array index would be different from current usage
of the spread operator.
But it could be a nice alternative to
$arr = [...$arr, $arr2], as it
does not require to repeat the first variable.
So I am not against it.
--- Andreas
On Thu, 6 Apr 2023 at 00:36, Juliette Reinders Folmer
<php-internals_nospam@adviesenzo.nl> wrote:
>
> On 6-4-2023 0:12, Vorisek, Michael wrote:
> > Hello,
> >
> > I would like to open a discussion for
> > https://github.com/php/php-src/issues/10791 .
> >
> > [https://opengraph.githubassets.com/a23cb565cc8acac6a33ecab5d9ee68a46f046a1ffe215501673156e506695430/php/php-src/issues/10791]<https://github.com/php/php-src/issues/10791>
> > Array spread append · Issue #10791 ·
> > php/php-src<https://github.com/php/php-src/issues/10791>
> > Description Currently spread operator can be used for almost anything. But not for array
> > append. I propose the following to be supported: <?php $arr = [1, 2]; $arr2 = [3, 4]; $arr[...] =
> > $arr2; // ...
> > github.com
> > Appending N elements to an array is quite common language usage pattern and I belive it
> > should be supported natively for shorter syntax, language consistency and performance.
> >
> > I am unable to implement it, but I am ready to help with RFC.
> >
> > Michael Vorisek
>
> I have the feeling I'm missing something, but how would this compare to
> array join (which is already available and is actually shorter than this) ?
>
> $arr += $arr2;
>
> Smile,
> Juliette
>
>