Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120496 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 91068 invoked from network); 31 May 2023 23:14:12 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 31 May 2023 23:14:12 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id EA20B180538 for ; Wed, 31 May 2023 16:14:11 -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,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-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) (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:14:11 -0700 (PDT) Received: by mail-wr1-f50.google.com with SMTP id ffacd0b85a97d-30ad458f085so1240836f8f.0 for ; Wed, 31 May 2023 16:14:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=datadoghq.com; s=google; t=1685574850; x=1688166850; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=RG+d2Ha50Uyx4rEMHJQU3aRlUrdUc2iB5tXP1PLN4Cs=; b=XyOwUKj/7XUH9PkJeKWbk0I6PEf3LOH42JGNKj1yNm9I4M2q7/6wO2LWi/OZ2nYVzj jlrIeHcLX8PQ9j5SL4MArJh23aPf8WVnBhqRwSQmas3fxtwOfhpjh/EAW3DQeNYKeL/l 5z8QMel1+Luq3blqJgwwFsgnMEycfiMo0p3Ak= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685574850; x=1688166850; h=content-transfer-encoding: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=RG+d2Ha50Uyx4rEMHJQU3aRlUrdUc2iB5tXP1PLN4Cs=; b=GwlpoUPpFxUSiRCGaHGFuoGGJnqRKAlAoDPque4/n8Gf6pUZ8H5Uy4NflkneUQn3NJ PvOyZ2AWSjZ/Vi9ouNK+g81+/x9yqxVsGRJ8pCU+38NgMnxPt4XuczYItFM0SQ2q8ikF +sCJiTt2WYo8ipofSwEsYXJi8qvpfJ51SDZYlLS9wdIxfSy1WN0AzWYlpSleVpW9X+Fi l1gmjE1N0K1B3sn9xPD0oz/k2NdY5kvyt8f0nTVss4nzezRD4ZAbuki2gX8Pk/5a7i/F Um+3oXFYRJRiBdXjZCxgZ8NrT0Pr2VVaVObzHVW6U8p2b8QvPUVAn8ePa+gAPfV2331e MTig== X-Gm-Message-State: AC+VfDylClIHmSqOFOS1IzTRIx7nF219SoFm7AgqzpLWEOyPUpogzR6v 3v8+jjbyiDncwLlDMyh8UgsZKvTQnwRu4fnlttFP5A== X-Google-Smtp-Source: ACHHUZ6cTSH+n76HD5DoVSjkoEXuqkbCUuofqxQwk5I7IswW408MEw+UW1pDHIlI4lsB2sOJlWjPje3pM87BAI4vI7U= X-Received: by 2002:adf:efc9:0:b0:30a:d9e6:7acd with SMTP id i9-20020adfefc9000000b0030ad9e67acdmr52284wrp.6.1685574849984; Wed, 31 May 2023 16:14:09 -0700 (PDT) MIME-Version: 1.0 References: <289E585B-EF8B-4B17-89BE-BE8295FD9FE1@gmail.com> In-Reply-To: <289E585B-EF8B-4B17-89BE-BE8295FD9FE1@gmail.com> Reply-To: Levi Morrison Date: Wed, 31 May 2023 17:13:58 -0600 Message-ID: To: Boro Sitnikovski Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] [RFC] [Discussion] Add new function `array_group` From: internals@lists.php.net ("Levi Morrison via internals") On Tue, May 30, 2023 at 5:35=E2=80=AFAM Boro Sitnikovski wrote: > > Hello all, > > As per the How To Create an RFC instructions, I am sending this e-mail in= order to get your feedback on my proposal. > > I propose introducing a function to PHP core named `array_group`. This fu= nction takes an array and a function and returns an array that contains arr= ays - groups of consecutive elements. This is very similar to Haskell's `gr= oupBy` function. > > For some background as to why - usually, when people want to do grouping = in PHP, they use hash maps, so something like: > > ``` > $array =3D [ > [ 'id' =3D> 1, 'value' =3D> 'foo' ], > [ 'id' =3D> 1, 'value' =3D> 'bar' ], > [ 'id' =3D> 2, 'value' =3D> 'baz' ], > ]; > > $groups =3D []; > foreach ( $array as $element ) { > $groups[ $element['id'] ][] =3D $element; > } > > var_dump( $groups ); > ``` > > This can now be achieved as follows (not preserving keys): > > ``` > $array =3D [ > [ 'id' =3D> 1, 'value' =3D> 'foo' ], > [ 'id' =3D> 1, 'value' =3D> 'bar' ], > [ 'id' =3D> 2, 'value' =3D> 'baz' ], > ]; > > $groups =3D array_group( $array, function( $a, $b ) { > return $a['id'] =3D=3D $b['id']; > } ); > ``` > > The disadvantage of the first approach is that we are only limited to usi= ng equality check, and we cannot group by, say, `<` or other functions. > Similarly, the advantage of the first approach is that the keys are prese= rved, and elements needn't be consecutive. > > In any case, I think a utility function such as `array_group` will be wid= ely useful. > > Please find attached a patch with a proposed implementation. Curious abou= t your feedback. > > Best, > > Boro Sitnikovski > 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.