Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:131060 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id 1039C1A00BC for ; Fri, 29 May 2026 16:04:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1780070698; bh=bOG0F79AcmWrFWafuDWcZa4551I44s3/VpoL+Xu8nOg=; h=Date:From:To:In-Reply-To:References:Subject:From; b=W3tcYmhaCz3kaMf9QU3M1Q9XCYa2WQKW4PNMeD9DV+4PfEdjsvWS3GMcgq3lSs+cD rTlpcR1khJqVYghk+Fq6A6B7jX1HpkwPKAM7pxh2PH+JCdBmy0AQbXXiE+CTODMVAm FGsmaXsMYWBC3ABsqi1ktK98JowluavmkjLIXVpLLvpfOLPIoOjnFZ3ENzV7KymJr5 2iKHrgGu22zRmANhallHhPMEZWsjJWUQwpPrG5i3Tf62okVWRjaCC5Yr/B1dtcAbEd kvnaS6O4CsET86NuJ/zd7HEiid3XbZDxvkj4vcbFDb5vwwlJXbngaeSBGgVALMbA40 5jccMwvDAJDdg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D6BAD1805DC for ; Fri, 29 May 2026 16:04:57 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from fhigh-b2-smtp.messagingengine.com (fhigh-b2-smtp.messagingengine.com [202.12.124.153]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 29 May 2026 16:04:57 +0000 (UTC) Received: from phl-compute-04.internal (phl-compute-04.internal [10.202.2.44]) by mailfhigh.stl.internal (Postfix) with ESMTP id 69FB27A0049 for ; Fri, 29 May 2026 12:04:52 -0400 (EDT) Received: from phl-imap-02 ([10.202.2.81]) by phl-compute-04.internal (MEProxy); Fri, 29 May 2026 12:04:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to; s=fm3; t=1780070692; x=1780157092; bh=1IB7z8+OHb/6NYfP2TP/h qlKAsNGfER9SAxRKxHGu2E=; b=EZwhbKZcOKdOOOdPyiDTIyGeMZ/x9xaZDUCpV dZNBVpAckcJV3HnrUasqPSFqEmv6Yft4dM7b9Ir2Z7Hx/Ul2Cn37DgCvZaxouieG a/2qjyM9BgRhvOTgnVY4K8P77fVdPFV/NUKTvtVY7z0VtInpHf0QiBrHVU0S5X+c TONygLOa7FWUawI53DytI0cjqjJWJVJyO+fQ3cx6CbrVBp5lfistlxfOVAO8eamR ArpdJdtfpdsYH/Jz0YHoLhQsv6ggtLEbUavuQdpQVV3vAA1hMvLTxPHni3tR7f13 M6s1q/5PjCDDCHpMzYss+cyPzojDrLKr+cI+XhfrTLZYGFvIw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; t=1780070692; x=1780157092; bh=1 IB7z8+OHb/6NYfP2TP/hqlKAsNGfER9SAxRKxHGu2E=; b=YzfG9mkDkb0j8PdCJ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ail Received: by mailuser.phl.internal (Postfix, from userid 501) id CABD8700065; Fri, 29 May 2026 12:04:51 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 X-ThreadId: AOnToJ90c1og Date: Fri, 29 May 2026 11:04:30 -0500 To: "php internals" Message-ID: In-Reply-To: References: Subject: Re: [PHP-DEV] [Pre-RFC] Null coalescing associative array elements Content-Type: text/plain Content-Transfer-Encoding: 7bit From: larry@garfieldtech.com ("Larry Garfield") On Fri, May 29, 2026, at 4:31 AM, Alexander Egorov wrote: > Greetings, internals! > > In my practice, it is a very common case when I need to create an > associative array with varying structure, by that meaning that some > fields in the array are optional and are set conditionally. And of all > conditions the most frequent is checking for null-values: if the value > is null, then this field should be omitted from the final result. > > Unfortunately, PHP currently does not offer any shorthand syntax for > declaring such fields in a compact way. In most cases for each of such fields > we need to have a dedicated "if", like: > > // $array is first filled with all required fields > // Then we add all optional fields based on some condition > if ($value !== null) { > $array['field'] = $value; > } > > This often leads to cumbersome code. > > My proposal, which already has a prototype implementation, is following. > When declaring an associative array, add a new null-coalescing double > arrow for the fields which should be omitted if the value is null: > > $array = [ > 'field1' => $param1, > 'field2' => $param2, > 'field3' ?=> $param3, > ]; > > In this case, if $param3 === null, the created array would only > contain 'field1' and 'field2' from start, without the need to later > get rid of 'field3'. > > This syntax will also allow more generalized conditions. For example, > instead of: > > if ($someCondition) { > $array['field'] = $value; > } > > You could create such array from start, if you are OK with such code-style: > > $array = [ > // other fields > 'field' ?=> $someCondition ? $value : null, > ]; > > The token '?=>' given here is mainly just an example, because the > exact form should be discussed anyway (this one, as well as others, > have pros and cons). > > So, before I create an RFC for this feature, I would like to get > initial feedback: would this feature be welcome in general? > > Prototype implementation for it: > https://github.com/Amegatron/php-src/tree/conditional-array-elements > > Or just the diff with current master: > https://github.com/php/php-src/compare/master...Amegatron:php-src:conditional-array-elements > > This is still a prototype though, some additional work must be done of > course, not counting the tests. > > A slightly bigger example: > https://gist.github.com/Amegatron/06da8770e46e116b05f1c290cb0d56fc > > Looking forward to your feedback. > > Cheers, > Alexander Egorov. My first thought is that the current code, which assigns potentially null values, works fine if you throw an array_filter() at it at the end. $array = [ 'field1' => $param1, 'field2' => $param2, 'field3' => $param3_which_is_null, ]; $a = array_filter($array); // $a now omits the null fields My second thought is that this is yet another reason why using associative arrays as if they were a data structure is wrong and should be avoided; just use a class and everything will be fine. If you need to serialize it later, there's many serializers on the market if JsonSerialize isn't sufficient. So this doesn't seem like a worthwhile addition to me. --Larry Garfield