Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116099 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 75191 invoked from network); 19 Sep 2021 13:04:03 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 19 Sep 2021 13:04:03 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4B2A4180546 for ; Sun, 19 Sep 2021 06:44:31 -0700 (PDT) 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.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS11403 66.111.0.0/20 X-Spam-Virus: No X-Envelope-From: Received: from out5-smtp.messagingengine.com (out5-smtp.messagingengine.com [66.111.4.29]) (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 ; Sun, 19 Sep 2021 06:44:30 -0700 (PDT) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 112135C00E5 for ; Sun, 19 Sep 2021 09:44:30 -0400 (EDT) Received: from imap43 ([10.202.2.93]) by compute1.internal (MEProxy); Sun, 19 Sep 2021 09:44:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=i7UqoM qJ4qESfiYC67vMddcVngUo575HGDMY7aPjSOI=; b=DxS9XTjKL2/ulfHVXcm2x4 me/JvP7IU+Kf4ePvP5Mn0Rf3Y7zhPcHRdG0JV9R2lowLtzdUX0S+zoc5TnvZf+S0 pgkWoBwGuycFdLvaH4yxR4PiroUVQaq12mRo+W+x89FGBtUKREAgeY3Rsag9jj+s E0I7MjKEHh90QyiTIbWR9Hhk6nKAN+PdE/4C7RtgStp7FyJYXNFaIu0rrbCLlTrp 4AvEqVJenbmujoJljjWnzE/1bYq4DG8CbQbig/wUzgZB5hRRzQrT/DX831bugeEL yIthugJQgyFx+94CrT/KbAIwYRN3cIwtMqmuryBpLljcJNdcXnX5ncxUTT9u96ew == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvtddrudeitddgieekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgesthdtredtreerjeenucfhrhhomhepfdfnrghr rhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtoh hmqeenucggtffrrghtthgvrhhnpeeggfeuledukeffleefffdtjeeludeuvdeiffdtudeg geevjeejffelhedvgeekffenucffohhmrghinhepphhhphdrnhgvthenucevlhhushhtvg hrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehlrghrrhihsehgrghrfhhi vghlughtvggthhdrtghomh X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id B02C8AC0362; Sun, 19 Sep 2021 09:44:29 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.5.0-alpha0-1291-gc66fc0a3a2-fm-20210913.001-gc66fc0a3 Mime-Version: 1.0 Message-ID: In-Reply-To: References: Date: Sun, 19 Sep 2021 08:44:09 -0500 To: "php internals" Content-Type: text/plain Subject: =?UTF-8?Q?Re:_[PHP-DEV]_Proposal:_Adding_an_ARRAY=5FFILTER=5FREINDEX_fla?= =?UTF-8?Q?g_to_array=5Fvalues?= From: larry@garfieldtech.com ("Larry Garfield") On Sun, Sep 19, 2021, at 8:11 AM, tyson andre wrote: > Hi internals, > > Currently, array_filter will always return the original keys. > This often requires an additional wrapping call of > array_values(array_filter(...)) to reindex the keys and return a list. > (or applications may not realize there will be gaps in the keys until > it causes a bug or unexpected JSON encoding, etc.) > > PHP is also more memory/time efficient at creating packed arrays than > it is at creating associative arrays. > > What are your thoughts on adding `ARRAY_FILTER_REINDEX`, to ignore the > original int/string keys and replace them with `0, 1, 2, ...` > > ``` > php > echo json_encode(array_filter([5,6,7,8], fn($value) => $value % 2 > > 0)); > {"0":5,"2":7} > // proposed flag > php > echo json_encode(array_filter([5,6,7,8], fn($value) => $value % 2 > > 0, ARRAY_FILTER_REINDEX)); > [5,7] > ``` > > https://www.php.net/array_filter already has the `int $mode = 0` which > accepts the bit flags `ARRAY_FILTER_USE_KEY` and `ARRAY_FILTER_USE_BOTH` > These could be then be combined with the proposed bit flag > `ARRAY_FILTER_REINDEX`, e.g. to filter an array based on both the array > keys and values, and return a list without gaps. > (and if $callback is null, this would return a list containing only the > truthy values) > > Thoughts? > > Thanks, > Tyson In cases where I do want it reindexed, the existing array_values() is fine in most cases. Creating small, purpose-built tools that are easily composed is generally the right strategy. The only time I could see a flag being a better alternative is when the memory difference would be huge. Do we have a sense for how much of a memory difference we'd be looking at by combining it into a single operation? (There's also the grossness of nesting functions inside each other, but the answer to that is pipes, not more flags.) --Larry Garfield