Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120497 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 92657 invoked from network); 31 May 2023 23:19:33 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 31 May 2023 23:19:33 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 82272180089 for ; Wed, 31 May 2023 16:19:32 -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.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,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-ej1-f54.google.com (mail-ej1-f54.google.com [209.85.218.54]) (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, 31 May 2023 16:19:32 -0700 (PDT) Received: by mail-ej1-f54.google.com with SMTP id a640c23a62f3a-97448470403so6449366b.1 for ; Wed, 31 May 2023 16:19:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685575171; x=1688167171; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=QCDwEBVegZ0HPZ28CGU0FGhq3OQH3T66mMb8nY0MkyQ=; b=qXypk0PkLOvf5GJh/REmaJCaR5+l+7SYBr0LKlGtkdP2hmr5bMjVTexHJFhCeCXALK L9efEz3kGw8kD1KdNVwVVaaYuKfgIi/LnwtuuVCkti1/w36dqJMSUbfuDWcNpbBu5JdU MWMbz7LxBXEFruxDfdaLrM+CHGBTihu1J1AKznyPz3WF5C5oMTXLsBvIbXEtxxQI6DHG NEkVoj/vUxyxBcXHC03ClAUSK8FV2SZhBtuBwIiekj8uroHhr/1zvtfpDlcSoXskUn0W oaCeb3+b4gyvPtLEmM/zt5wfnFGcjGcwyFOJrVEoeyVzk+P7/mVFd6Aei4mq3E4UzW7l jorg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685575171; x=1688167171; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QCDwEBVegZ0HPZ28CGU0FGhq3OQH3T66mMb8nY0MkyQ=; b=joS7OJUWdyk2scYKNi9b6L/RnBxGErabKV5PbXwO5sTehvF4BMj3uGFpzL6HtOhZJA xDnLBx3yex4vWJoarzpxdHn74VHBlYpFoGZzyBa29jSS2YQeTXMkveUPLuZg1hTsPOkC 2h4c1MgUcGYJuEVGGc/Yl+a5/KQSCVdlPkcyqJoot29CE0nuOvMQ+dDhj9eZ/gH0f38j qRiQ1UlSnCF+4PE2F4Ra5/Y3Jn6y7ilOKSNd5CvQZ7KgcP7/IVe0LACz4hza7lUb0qnR bDTWSi5gLhaeUgdGV7Q9THbPTtsMLWmbhbARHpkOO1BeHGXjbbSpOG71ovNt54k5OoVH mJVg== X-Gm-Message-State: AC+VfDyZD09pgvmP9xwJfbU6KLDV8GODETZaUe34GEDkqwro/rXThLIH jAZBbfVsyTOrJA+Ek8AxPuQ= X-Google-Smtp-Source: ACHHUZ4uLhvE4/uaDG6K5HnEn1Hcfpfr6xYlf1Q6ulB1Ct5bfTMulyDsDhf5f+DUd1HFSqUFhn6/lw== X-Received: by 2002:a17:907:86ab:b0:96f:d345:d104 with SMTP id qa43-20020a17090786ab00b0096fd345d104mr5431514ejc.38.1685575170680; Wed, 31 May 2023 16:19:30 -0700 (PDT) Received: from smtpclient.apple ([46.217.49.136]) by smtp.gmail.com with ESMTPSA id gz15-20020a170906f2cf00b00965ac8f8a3dsm9792719ejb.173.2023.05.31.16.19.29 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 31 May 2023 16:19:30 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.600.7\)) In-Reply-To: Date: Thu, 1 Jun 2023 01:19:19 +0200 Cc: internals@lists.php.net Content-Transfer-Encoding: quoted-printable Message-ID: <5FE9216F-A84D-4482-A22E-05A44C2A9A3C@gmail.com> References: <289E585B-EF8B-4B17-89BE-BE8295FD9FE1@gmail.com> To: Levi Morrison X-Mailer: Apple Mail (2.3731.600.7) Subject: Re: [PHP-DEV] [RFC] [Discussion] Add new function `array_group` From: buritomath@gmail.com (Boro Sitnikovski) > On 1.6.2023, at 01:13, Levi Morrison = wrote: >=20 > On Tue, May 30, 2023 at 5:35=E2=80=AFAM Boro Sitnikovski = wrote: >>=20 >> Hello all, >>=20 >> As per the How To Create an RFC instructions, I am sending this = e-mail in order to get your feedback on my proposal. >>=20 >> I propose introducing a function to PHP core named `array_group`. = This function takes an array and a function and returns an array that = contains arrays - groups of consecutive elements. This is very similar = to Haskell's `groupBy` function. >>=20 >> For some background as to why - usually, when people want to do = grouping in PHP, they use hash maps, so something like: >>=20 >> ``` >> > $array =3D [ >> [ 'id' =3D> 1, 'value' =3D> 'foo' ], >> [ 'id' =3D> 1, 'value' =3D> 'bar' ], >> [ 'id' =3D> 2, 'value' =3D> 'baz' ], >> ]; >>=20 >> $groups =3D []; >> foreach ( $array as $element ) { >> $groups[ $element['id'] ][] =3D $element; >> } >>=20 >> var_dump( $groups ); >> ``` >>=20 >> This can now be achieved as follows (not preserving keys): >>=20 >> ``` >> > $array =3D [ >> [ 'id' =3D> 1, 'value' =3D> 'foo' ], >> [ 'id' =3D> 1, 'value' =3D> 'bar' ], >> [ 'id' =3D> 2, 'value' =3D> 'baz' ], >> ]; >>=20 >> $groups =3D array_group( $array, function( $a, $b ) { >> return $a['id'] =3D=3D $b['id']; >> } ); >> ``` >>=20 >> The disadvantage of the first approach is that we are only limited to = using equality check, and we cannot group by, say, `<` or other = functions. >> Similarly, the advantage of the first approach is that the keys are = preserved, and elements needn't be consecutive. >>=20 >> In any case, I think a utility function such as `array_group` will be = widely useful. >>=20 >> Please find attached a patch with a proposed implementation. Curious = about your feedback. >>=20 >> Best, >>=20 >> Boro Sitnikovski >>=20 >=20 > I'm sorry if one of the many replies already mentioned this, but there > was a failed RFC for `array_group`: > https://wiki.php.net/rfc/array_column_results_grouping. Note that I > voted against it for technical reasons (the signature was just awful, > we can do better), but I am not against the idea of adding such helper > functions in principle. Folks did mention that RFC, but nobody mentioned the rejection reasons = specifically. Thank you for the information and encouragement! I was a = bit scared of this one being rejected too, but still decided to give it = a shot :)=