Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:131049 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 B609F1A00BC for ; Fri, 29 May 2026 09:32:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1780047137; bh=tBaLr8HbhQ/7cpnnpXTJzb7bne5HFuIHym6YAasPG4k=; h=From:Date:Subject:To:From; b=DRo/akejSiMIr0/Q1NALfVsheBM98PWJGT+Cvs5FeVa7NQ4ez9Ucw82slClAki/9A 5ZI59DBzi6Qai/9DtijC3xwvqucCjz0BX/dGNSILObtSudM7va+joXiziYMp692M6D 9WJIw5jwxhw6KIEFLqBdDgxOeFpEnvfDYKVMfzk5bkOfGLkgIf21DzQ1dJSTj7z7SH ZAEhLmiw7rxd1mWJvhX6Yel9YA2cbyRz+LzwLoX6eEd84OB1+h8oFKFxK80VdZ+JkM yy5n8GArzBDjwJCV7xbpdPL757Gyy0qFpAuxwstbPK9d6LZ/Mp3u1TySFmFDVQGWKU DtNkWvFnalJrg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 41A0F18002E for ; Fri, 29 May 2026 09:32:16 +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.9 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS, FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from mail-qv1-f46.google.com (mail-qv1-f46.google.com [209.85.219.46]) (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 09:32:16 +0000 (UTC) Received: by mail-qv1-f46.google.com with SMTP id 6a1803df08f44-8ccda0ac4fcso14464216d6.2 for ; Fri, 29 May 2026 02:32:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1780047130; cv=none; d=google.com; s=arc-20240605; b=TANtiSvsuL1YsuVKo6vmU4g1exVPpw9jolvw89ej7K6F/hy3YGnzUNJ9dGjF3oo4Xt F3BBxQeqhbLuSDRjfX5N4wIzEd43Cny8UyWG47IxOMpgCJnJk06LgRvJ+ftY5qELl3Cv J3qvh2EnLj3C2frxRYdjgFccaZY/YMDapAZTEnByqaZKWNKZywYETEaoesQTo7E60Jwy 7I05LCVftJ4Lq/bzmLa1ybu3D8jgEqsPgig8hyOLRKexSyDcz0/601ZI8z8fDfLwq1pR zwW95JfLL5MaxDVQjvXE3bFCdEpoap3HjOMlFWJwEKer/6rxnt+nTg4K/Iki5AuhfG5p YmAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=to:subject:message-id:date:from:mime-version:dkim-signature; bh=OT04QzuOqvYuXqUE2t+ZGdw9UB+0CIfqV1sp1umn4NE=; fh=5KvmJ5K5QBymFDNCdtgpPKEwgSxqOQnCzLzHVTYb8GE=; b=JGMoBPsfGADLEqszf5eSutQyRrGwyqd1m5lz6RQAtTSp3LV6FA21XXDXQv+2ObYUQe c/Tu15NZ5FC+GqyE08mn4F+uGieCm/BGotAvrBfhskQvfJ443RUVWbqepCKta//4FA/2 Z8KsKH/3FfGcwNCjMU+/p5PaaraGWTCd/G3BHyaJgPfprI3sazhaaKL/2C2C22aYfjzr +34zXinx5tLzykZ/ZYPhEiGpCDlRFJZi1njQM0tgrDEzWuuqo5VqmS4d26r+r4vcTB5M NFi8LevO3CMplMGvfwemC0uM1McvVYspXmVwXGHSPvD5L2kfF+0J7LR7rc3Uq+BZIYhL 5b6w==; darn=lists.php.net ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1780047130; x=1780651930; darn=lists.php.net; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=OT04QzuOqvYuXqUE2t+ZGdw9UB+0CIfqV1sp1umn4NE=; b=RsK8nEhhc1E77+IQeRIYWqdjBPcRSmaghg1hJB4CXzlYaK3wBlrKq541N0a7qAhnLY 8rolHO2asGeoguZBDETBOI2PnedDE0PJ8QnBk6a2G1K+1fjPXyvAbPa6BHAxRARUb06X Hc+zXdlz0YaY0Y/QAYOVorQNVRCZiEgZDQAwnggz0xQofx7TQYOQabKS57qr4bSkgmOE oxT38OUVVhtf4r3+zwPTAAhP0e7lxhkf0TibQFSf56ulU1BY6L3q32qVt81Oh8b+BGBm I+54XSlI87mLqV391R6qlxCd4JZJoS47R+paXh0RNBXMCy6vD6xp3VVugJS1vxoQWxa+ gWHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780047130; x=1780651930; h=to:subject:message-id:date:from:mime-version:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=OT04QzuOqvYuXqUE2t+ZGdw9UB+0CIfqV1sp1umn4NE=; b=VIB585/6fxtdTUOAnAPlR+tyLQUQsUy041RRppLRw+SQMooVxDEYHp382E/vb3bCGn oDluqyV7ck98OEVKxvGGE6+wC7d0mQESmjbLa2FJGqXmiSwZQkxR6hD0wmmvC/o5bOzs K7e9SDpJAAurfzfluAQ17NjFl5z0KVCgi3GIxBXAfc6C3ppvqRfxwP75zft7fHQZCwvH ys8N8zK16l7dMH5k7ihvM9nZGQuhRgn/3Zz0XVhkycl8Z7qbtgi+AXjpNwee7hhiV3YP lBPLI5BpRpJDm4pMwBEw/cM+lhAzuo0wXgs0Hff6LE3JXzaim2HJT8TbyYfuibLGCYBz 544g== X-Gm-Message-State: AOJu0Yxd9pbwk/eld1ZoJkMlM2gPJ7pkRe4A9IzVhrI4jixa/M+nLyio vZ8ofi00wAe9mod2XWbg++rkUvL/qpY9JAbLphoxe6ZIgaFKc8tNXg0swggfgRiwJldY+et4Q35 NYC4sXWFJGy1flUegKGTi5eHHACPdY/OLAjN5kIk= X-Gm-Gg: Acq92OE7em9h1NMkvwNBnnKAplsBZymrj8zeS8hp605wp+R3SqDAp6cbkgUfaQPZRIU 67KJwGPb+WLoZov/O9LzUx55jhfvQGkX/GTrPjku4gzvIJWehehHcFAYoDSQvSpWooKvWrLpXyW sp7i23kxg+Ra4uzvtXk8h6Gngnkeg+CvCyA1NTataX6mxlSSQk2IO0rQbtlA3FKBdP0tXlEAIHK H0qqXpo8kbauudPLrg7a65vE4L1xNKlkAProxX+eOTeCFRiiIscrV14kX4Kb3T/LvyhMVa0nRgX loN8R9FH6Z6jYZHLFEScrO1/HmpbGcvTg4/OzHQn+XOAU52e X-Received: by 2002:a05:6214:230b:b0:8cc:e659:d212 with SMTP id 6a1803df08f44-8cce80202a1mr21512786d6.26.1780047129849; Fri, 29 May 2026 02:32:09 -0700 (PDT) Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 Date: Fri, 29 May 2026 12:31:58 +0300 X-Gm-Features: AVHnY4KCbSc569x_LYgPClx6fLR0HIV2XT2Gp4tmuM0_9S8vn8fZsS4MgnHXuE4 Message-ID: Subject: [PHP-DEV] [Pre-RFC] Null coalescing associative array elements To: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" From: amego2006@gmail.com (Alexander Egorov) 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.