php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #79285 Allow unpacking of arrays with string keys into other arrays
Submitted: 2020-02-19 15:13 UTC Modified: 2020-02-19 21:28 UTC
From: caelan89 at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 7.4.2 OS: n/a
Private report: No CVE-ID: None
 [2020-02-19 15:13 UTC] caelan89 at gmail dot com
Description:
------------
You've added in array unpacking inside of arrays.

However, it is a half-baked solution (if you can even call it that), and relatively useless except for a few use cases.

You do not allow this:

$a1 = [
  'test' => 1
];

$a2 = [
  'test-2' => 2
];

$a3 = [
  ...$a1,
  ...$a2
];

There is absolutely zero reason, other than your inconvenience building it, as to why this should be forbidden. None at all.

It is semantically equivalent to:

$a3 = array_merge($a1, $a2);

Why on earth should I not be able to do the same with with the much neater and preferable array unpacking syntax?

Test script:
---------------
$a1 = [
  'test' => 1
];

$a2 = [
  'test-2' => 2
];

$a3 = [
  ...$a1,
  ...$a2
];


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-02-19 15:58 UTC] cmb@php.net
-Package: PHP Language Specification +Package: Scripting Engine problem
 [2020-02-19 18:57 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2020-02-19 18:57 UTC] requinix@php.net
https://wiki.php.net/rfc/spread_operator_for_array#string_keys
> In order to make the behavior consistent with argument unpacking, string keys are not supported.
 [2020-02-19 20:53 UTC] caelan89 at gmail dot com
requinx,

Unpacking an array to pass arguments to a function dynamically is not the same thing as unpacking an array inside an array to merge in-place.
 [2020-02-19 21:10 UTC] requinix@php.net
Yes, you are absolutely right, the two actually are different things. Perhaps that would explain why the RFC used the words "consistent with" and not "is the same as".
 [2020-02-19 21:21 UTC] caelan89 at gmail dot com
Are there any downsides to allowing string keys when unpacking inside arrays?

I agree that allowing string keys for function arguments doesn't seem to semantically quite fit together. Indexes don't map to arbitrary strings.

But why not for spreading within an array?

You could then simultaneously define default values, user customisable values and fixed values all within the array syntax.

For example:

$custom = [
  'value' => true,
];

$values = [
  'value' => false,
  ...$custom
  'fixed-value' => $whatever
];

Indexed arrays would concatenate, like with array_merge.

String keys will overwrite, like with array_merge.

Does it not make far more sense for the semantics to match array_match, which, if you did not know PHP didn't support it, you would think it would?
 [2020-02-19 21:23 UTC] caelan89 at gmail dot com
Sorry, not array_match, array_merge.

I'm tired.
 [2020-02-19 21:28 UTC] nikic@php.net
-Block user comment: No +Block user comment: Yes
 [2020-02-19 21:28 UTC] nikic@php.net
Pro tip: Don't be a total dick in your bug reports, and you might get a more information reply next time.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 15:01:32 2024 UTC