Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120458 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 48137 invoked from network); 30 May 2023 14:57:34 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 May 2023 14:57:34 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3657D180544 for ; Tue, 30 May 2023 07:57:34 -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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE, 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-yw1-f177.google.com (mail-yw1-f177.google.com [209.85.128.177]) (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 ; Tue, 30 May 2023 07:57:33 -0700 (PDT) Received: by mail-yw1-f177.google.com with SMTP id 00721157ae682-565bd368e19so52318337b3.1 for ; Tue, 30 May 2023 07:57:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech-net.20221208.gappssmtp.com; s=20221208; t=1685458653; x=1688050653; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=dZMqflVQupRp7pDQY141btQZ9nGxU8sXKaqgXa84L6w=; b=TkkW6F35XoJXokxS8VZpS7UtAe3KhCAMPS79HEdKeE3MbB5OOYGhHYoKYpOL8/oE4E JUPEqn6DH0jDMVgvDstoFig4arRhTlz40rXbuJL+JkKSBZi5e+QFjxKoRdCeM/qC+8xN s/wP7UhCtnEmdYZJkNB7KQKr1g4B1maxGfAmPhQG1O/61A2GAgdLwzT/mpDYEfxKETOQ AA/4U7CNsHwgtsoCaxzMuLnQV7UJj5FRyYA6QWmCijb1/Rmw7vt36koGy0m+JGP1mz3z RwkC2JFFmAD/+IWmXVlIQouB9MjwmDx2hXZH6bU5ZMPdl+19u/oQrm90ugF1kuSWs4fg pDfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685458653; x=1688050653; 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=dZMqflVQupRp7pDQY141btQZ9nGxU8sXKaqgXa84L6w=; b=JIwoxeA/6rNkA2tq/darq5bp980oUiIxOl/oIGYq/X49ZtuUQs8lWa3j5Nsbppf/KD bbxJJ+n0YBITat5f1h2XcnBj+loSKjLKnEZTc+Ko2EIjJisCxPiQR3k0LGAKaPgxHIdj IdP3WyDg57QIdrq5tQYyA4reNOIepCElEtaxuoycRIxCMnQfYInc05uBaD2/yxHalpIj MqiAho9ckzlEc5Egw1jEdu0bDulhg0LSEJDBU2jmkjQd3/Dg+OO2naUUx1hDBms8KM8v 51WDAav50E9l9+zadiM9XAqg/4GFRRTn/CFzmACU0S97OzOYsZkAdqX+G798yMBzP4LF vX3A== X-Gm-Message-State: AC+VfDzTf2p5Ihtexzw9Zln1UdHf5h3tZEWGlsda+EC3mtGgqPyP6Znm QFP8Z9oxydokNQ3NbugAs9kKHoQ4ClMaH9SyQnlVl295xfz2u2e2BnQ= X-Google-Smtp-Source: ACHHUZ4qgdRsbdgfxFpld9lQIdzUeR9oyYh6BbUk6+J8O8FpUP16E7O4L4wwoti3S3KxHLVzBJmiBsc/obSfH5+w0LA= X-Received: by 2002:a81:8a85:0:b0:561:b5cc:e110 with SMTP id a127-20020a818a85000000b00561b5cce110mr2564455ywg.6.1685458652807; Tue, 30 May 2023 07:57:32 -0700 (PDT) MIME-Version: 1.0 References: <289E585B-EF8B-4B17-89BE-BE8295FD9FE1@gmail.com> In-Reply-To: <289E585B-EF8B-4B17-89BE-BE8295FD9FE1@gmail.com> Date: Tue, 30 May 2023 16:57:22 +0200 Message-ID: To: Boro Sitnikovski Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] [RFC] [Discussion] Add new function `array_group` From: andreas@dqxtech.net (Andreas Hennings) Hello Boro, I think you should include the "expected result" in your code examples. Maybe this is in your patch file, but I don't think we want to look at that for discussion. Cheers Andreas On Tue, 30 May 2023 at 13:35, 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 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. > > For some background as to why - usually, when people want to do grouping in PHP, they use hash maps, so something like: > > ``` > $array = [ > [ 'id' => 1, 'value' => 'foo' ], > [ 'id' => 1, 'value' => 'bar' ], > [ 'id' => 2, 'value' => 'baz' ], > ]; > > $groups = []; > foreach ( $array as $element ) { > $groups[ $element['id'] ][] = $element; > } > > var_dump( $groups ); > ``` > > This can now be achieved as follows (not preserving keys): > > ``` > $array = [ > [ 'id' => 1, 'value' => 'foo' ], > [ 'id' => 1, 'value' => 'bar' ], > [ 'id' => 2, 'value' => 'baz' ], > ]; > > $groups = array_group( $array, function( $a, $b ) { > return $a['id'] == $b['id']; > } ); > ``` > > 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. > > In any case, I think a utility function such as `array_group` will be widely useful. > > Please find attached a patch with a proposed implementation. Curious about your feedback. > > Best, > > Boro Sitnikovski >