Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116579 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 8340 invoked from network); 6 Dec 2021 23:59:54 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 6 Dec 2021 23:59:54 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C31BF1804DF for ; Mon, 6 Dec 2021 16:59:58 -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.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: AS29838 64.147.123.0/24 X-Spam-Virus: No X-Envelope-From: Received: from wout4-smtp.messagingengine.com (wout4-smtp.messagingengine.com [64.147.123.20]) (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 ; Mon, 6 Dec 2021 16:59:58 -0800 (PST) Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id E46643201D0E for ; Mon, 6 Dec 2021 19:59:56 -0500 (EST) Received: from imap43 ([10.202.2.93]) by compute6.internal (MEProxy); Mon, 06 Dec 2021 19:59:57 -0500 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=fm1; bh=qRM9jX O8d/cvskrTVcAkIt+asTa2LsgR5LcstMmliVs=; b=KTfH4JBkPfIelcNuTvLIpI jzURBrjKVklQWbQN055GEKaN2OIbXfRZSVGT2nC9L/HXSRUkXGNtKr4r3ile+lNB mNSwMVS12wNqfQhClDhepFzO7VXLIpht5t74Gc9tgTNVyCgr1ILHezzBOXp1stf2 XCRkClIwzNVnExuckkDaWkMALEo4QlTvrTN+m7zdxdfbyaYJxzqcX6BWao7C1Kcb V/tcdw0i3AcR6zrlImB7eHqs5twVEQJ2ulJbV1gFyv22dJYuICDfrOkRRrqWXpa6 uUqCMXJonQW9/zt4CLUyLdzveG7v7fOp8nLY3Udrw8u5uCpdgsKcJ8gQdZJXy3Rg == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvuddrjeeggddvlecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefofgggkfgjfhffhffvufgtsehttdertderredtnecuhfhrohhmpedfnfgrrhhr hicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtghomh eqnecuggftrfgrthhtvghrnhepgeelgfekudeivddvteffueejffdthfejieevhefgffek udevkedtvdelvddvffefnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrg hilhhfrhhomheplhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhm X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id C1712AC03DB; Mon, 6 Dec 2021 19:59:55 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.5.0-alpha0-4458-g51a91c06b2-fm-20211130.004-g51a91c06 Mime-Version: 1.0 Message-ID: <2e4dd298-77a1-4c40-ba31-1c9b7ebe1bb1@www.fastmail.com> In-Reply-To: References: <61ac9759.1c69fb81.b0241.1e6fSMTPIN_ADDED_MISSING@mx.google.com> <268F5B4F-4770-48AA-B212-DE23116BCF4C@gmail.com> Date: Mon, 06 Dec 2021 18:59:35 -0600 To: "php internals" Content-Type: text/plain Subject: Re: [PHP-DEV] RFC [Discussion] array_column results grouping From: larry@garfieldtech.com ("Larry Garfield") On Mon, Dec 6, 2021, at 4:16 PM, Hendra Gunawan wrote: >> >> Hello Folks, Thanks a lot for your feedback, as already mentioned in >> the RFC and as mentioned by Rowan too a new function is an option. I >> think that mostly we will go with the new function option. >> I will try to edit the PR to add a new function, does there any >> suggestions/naming conventions for the new function? a colleague >> suggested being `array_group_by` and Hendra already suggested to be >> `array_column_group` which is good too. > > It would be better if You add another voting item for this one. In my opinion, > `column` is less important to be included, and `array_group` is sufficiently > descriptive. > > `reducer` is one of the candidates to be included. Additionally, there are > `mapper`, `filter`, and `sorter`. So Obviously we need a new function, rather > than modify the old one. > > To be clear and to make it simpler, all callbacks operate on the deepest > subgroup. > > Signature: > > reducer(array|object $item, int|string $index, mixed $cumulative): mixed > mapper(array|object $item): array|object > filter(array|object $item, int|string $index): bool > sorter(array|object $item1, array|object $item2): int > > A note in case you are able to implement them: > - `reducer` and `mapper` cannot coexist in a single function call. > - `filter` should be executed before `reducer` or `mapper`. > - `sorter` should be decided whether it executes before or after `reducer`/ > `mapper`: `sorter` is more suitable to execute before `filter`, but more > suitable after `mapper`. > > A note for consumers: > - `mapper` is designed to operate on the subgroup item, but it can be operated > on the field value of the item. > - filtering can be done inside `reducer` or `mapper`, but the number of > subgroup members is as it is. With dedicated `filter` supplied, we can > decrease the number. > - If You want to filter an intermediate group, You can use `unset` or `if-else` > > > $group = array_group($people, ["education", "sex"], FN_REDUCER); > foreach ($group as &$eduGroup) { > foreach ($eduGroup as &$sexGroup) { > unset($sexGroup["male"]); > } > } > > Not a perfect solution, but at least your code is reduced. Better map/filter/reduce primitives in PHP would be dandy, but they have nothing to do with array grouping. They're a separate operation that should be composed with grouping. Packing them all into a single function sounds like a terrible idea. If we wanted that... that's called list comprehensions, which I *would* support and even tried to rally support for a few years ago, but got met with a big shrug. :-) --Larry Garfield