Am 02.12.2020 um 18:24 schrieb Florian Stascheck <[email protected]>:
> I suggest to allow string keys to also be used in array literals:
>
> $template = ['created_at' => time(), 'is_admin' => 1];
> $db_rows = [
> ['name' => 'Alice', 'email' => '[email protected]', ...$template],
> ['name' => 'Bob', 'email' => '[email protected]', ...$template],
> ];
You can already easily enough do this with
$db_rows = [
['name' => 'Alice', 'email' => '[email protected]'] + $template,
['name' => 'Bob', 'email' => '[email protected]'] + $template,
];
so I'm not sure if it is really needed.
But then again I'm used to the +-operator for associative arrays and wouldn't
expect to be able to use the spread operator instead. Someone learning PHP now
might see things differently.
Side-note:
For the ...$template syntax I would assume that later values win (like ['foo'
=> "bar", 'foo' => "qux"] will result in ['foo' => "qux"]) so the exact
equivalent for your example would probably be
$template + ['name' => 'Alice', 'email' => '[email protected]'],
but in reality one often wants to be able to override template values, that's
why I wrote [ ... ] + $template and your example would be
[...$template, 'name' => 'Alice', 'email' => '[email protected]'],
- Chris
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php