Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112384 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 54350 invoked from network); 2 Dec 2020 21:51:57 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 2 Dec 2020 21:51:57 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 6354B1804C6 for ; Wed, 2 Dec 2020 13:19:43 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from darkcity.gna.ch (darkcity.gna.ch [195.49.47.11]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 2 Dec 2020 13:19:42 -0800 (PST) Received: from macbook-air.home (unknown [IPv6:2a02:1205:5053:a220:b9:9c2c:9367:e64b]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by darkcity.gna.ch (Postfix) with ESMTPSA id A364F6C052B; Wed, 2 Dec 2020 22:19:39 +0100 (CET) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.4\)) In-Reply-To: Date: Wed, 2 Dec 2020 22:19:38 +0100 Cc: PHP Internals List Content-Transfer-Encoding: quoted-printable Message-ID: References: To: Florian Stascheck X-Mailer: Apple Mail (2.3608.120.23.2.4) Subject: Re: [PHP-DEV] Suggestion: Inconsistency: Allow array spread operator to work on string keys From: cschneid@cschneid.com (Christian Schneider) Am 02.12.2020 um 18:24 schrieb Florian Stascheck = : > I suggest to allow string keys to also be used in array literals: >=20 > $template =3D ['created_at' =3D> time(), 'is_admin' =3D> 1]; > $db_rows =3D [ > ['name' =3D> 'Alice', 'email' =3D> 'alice@example.org', = ...$template], > ['name' =3D> 'Bob', 'email' =3D> 'bob@example.org', ...$template], > ]; You can already easily enough do this with $db_rows =3D [ ['name' =3D> 'Alice', 'email' =3D> 'alice@example.org'] = + $template, ['name' =3D> 'Bob', 'email' =3D> 'bob@example.org'] + = $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' =3D> "bar", 'foo' =3D> "qux"] will result in ['foo' =3D> "qux"]) = so the exact equivalent for your example would probably be $template + ['name' =3D> 'Alice', 'email' =3D> = 'alice@example.org'], 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' =3D> 'Alice', 'email' =3D> = 'alice@example.org'], - Chris