Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119445 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 60524 invoked from network); 1 Feb 2023 17:26:09 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 1 Feb 2023 17:26:09 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 87ACC1804F7 for ; Wed, 1 Feb 2023 09:26:06 -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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-yb1-f170.google.com (mail-yb1-f170.google.com [209.85.219.170]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 1 Feb 2023 09:26:05 -0800 (PST) Received: by mail-yb1-f170.google.com with SMTP id u72so23210829ybi.7 for ; Wed, 01 Feb 2023 09:26:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=YL2Ux7cl5vFz34qLttaHKGK9yoWbk78b+/OUWebmXGM=; b=Ldy24FEIS4Xer6eWvTWNnKwRCyFUAQNs3RKLZbHIPOmVvbNBW1+xyBnPMmpjAlqPw3 WVcmIW5JPCA8zfRcsQ+KmFPS1X/BX78XMll9NhTYfWCXsAE9gQSFGkSHZIM24vupBoJD 1EW/ietnZLs1eX+H4bmLqDx01Ay6a8fGOo09OaKDWeWjyxGBcJt8mFhblIr/UUlq3eFB smQG0qPInrtSRhkks9iOPXq2Drz+/CacKxtoh+QvXUhgBl7JXZPeyPvH45PEwy3S2IVK M9HVl6WGe2XCpTRXR8z6uUY+ywPymKaZaoKDkHw4KvVGk8DRQL05sX6ZuCM/1750veLx JgpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=YL2Ux7cl5vFz34qLttaHKGK9yoWbk78b+/OUWebmXGM=; b=VoypMPdE2YmMT+UAsQdfDBBChYfFlCFED4mSpl4wUaJ8M1mc0vIIfQ/1oeiUafTe0I CsLxs0Uk3flwVbQDKQxl+pcU/coMfEoLp3fyaNBjLxK2kSrRFxWw2WqetpmTcBHOffnH NvKDkeCoCfrC0Sy6ItSwlR/P/yv5XXlOo8nA+k/uRxoFOCDa4WZ6kerWYvnkLAiH7uB3 c8ec22tQF635cvNEw0tVu6RbD8jYJh0Ex0J404Ka+TjZ4iuNCdEV5n9AJzQRPH5AaRK8 VAxGLbZNYTSHUmP81MenxkLcIN5kamncr4M9mThEFt1B8tzPP+l2BSSjHy/F/4s/0YVL rX9Q== X-Gm-Message-State: AO0yUKXDEB7c65JEYuXGJ/r6lvaZQproHYF0hrepoDvYARe10XEFEfY6 amiU6JqsZ4Q+td8flGwgq9m6k6GpxWsn+G173OVO0omVGKt4bw== X-Google-Smtp-Source: AK7set9ZdinlQ5ONxSKnLkRtorrzLbOwa4NxAuSFkwek62k00A7JLmvdzUAPfjXCVmD4mPZf5aabQqaMwJGcwjfBeWA= X-Received: by 2002:a25:ada1:0:b0:839:c329:be37 with SMTP id z33-20020a25ada1000000b00839c329be37mr432864ybi.89.1675272365153; Wed, 01 Feb 2023 09:26:05 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 1 Feb 2023 18:25:53 +0100 Message-ID: To: Sergii Shymko Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary="0000000000003a262f05f3a6bea1" Subject: Re: [PHP-DEV] RFC proposal: function array_filter_list() to avoid subtle bugs/workarounds From: ocramius@gmail.com (Marco Pivetta) --0000000000003a262f05f3a6bea1 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hey Sergii, On Wed, 1 Feb 2023 at 18:22, Sergii Shymko wrote: > Hi, > > After programming in PHP for two decades, my goal for 2023 is to try to > contribute to the language. > The plan is to start small and, if successful, work my way up increasing > complexity of proposals. > This topic has been chosen for starters, because IMO it strikes a good > balance between simplicity and usefulness. > I should be able to implement the RFC myself, unless some deep OPcache/JI= T > nuances pop up. > > Let me give you a brief overview of the problem and the proposed solution= . > Function array_is_list() added in PHP 8.1 introduces the concept of a > "list" =E2=80=93 array having 0..count-1 indexes. > The function is awesome and array "lists" are completely compatible with > all array_* functions! > However, function array_filter() exhibits a nuanced behavior when > filtering lists. > For instance, it preserves array keys which may (or may not) create gaps > in sequential indexes. > These gaps mean that a filtered list is not a list anymore as validated b= y > array_is_list(). > > For example: > $originalList =3D ['first', '', 'last']; > $filteredList =3D array_filter($originalList); > var_export(filteredList); // array(0 =3D> 'first', 2 =3D> 'last') > var_export(array_is_list($originalList)); // true > var_export(array_is_list($filteredList)); // false > > The behavior is counterintuitive and can lead to subtle bugs, such as > encoding issues: > echo json_encode($originalList); // ["first", "", "last"] > echo json_encode($filteredList); // {"0": "first", "2": "last"} > > The workaround is to post-process the filtered array with array_values() > to reset the indexes. > The proposal is to introduce a function array_filter_list() that would > work solely on lists. > It will have the same signature as array_filter() and will always return = a > valid list. > > See a draft RFC with more details here: > > https://dev.to/sshymko/php-rfc-arrayfilterlist-function-35mb-temp-slug-70= 74000?preview=3D21d6760126a02464b0511498bbb95749150afb17a7ff6377c458ee54a8f= 57cfe00d4e258aa06bad3232c0dd9e73a2d62138fc990048987e9e2339a3d > > I just registered a wiki account "sshymko" with the intention of > submitting the RFC. > Could someone please approve the account and give it some karma? > > Looking forward to collaborating with the internals team! =F0=9F=99=82 > > I don't want to shoot this down too early, but: 1. why in the language, when a simple userland function suffices? 2. what's wrong with writing `array_values(array_filter(...))`? Marco Pivetta https://twitter.com/Ocramius https://ocramius.github.io/ --0000000000003a262f05f3a6bea1--