Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116568 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 55708 invoked from network); 4 Dec 2021 23:51:23 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Dec 2021 23:51:23 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E803B180211 for ; Sat, 4 Dec 2021 16:50:55 -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=-0.7 required=5.0 tests=BAYES_05,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS 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-qk1-f171.google.com (mail-qk1-f171.google.com [209.85.222.171]) (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 ; Sat, 4 Dec 2021 16:50:52 -0800 (PST) Received: by mail-qk1-f171.google.com with SMTP id g28so7569015qkk.9 for ; Sat, 04 Dec 2021 16:50:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=vEc1REouCHskATqJzYSjFWUiQy/G3hP9LkDdu0HAEDI=; b=i8renG1+259pCWxYD472tnD9Y8tzrSnjrVLtQWtQIR7ZXM7KQProxnXAc4CRzKdmW8 M8gbNlO/bqf8ubleYX9ZDXW0fUq4BWVzHEn3IQQnZROZLN8IWgi4Xw7O5ts0sKllkwCL vz5N1yLBjtXmoNwdVP6esElP1YbekeP+IXR2fH0C//+RzmcXY7IHDPriVZZvWSR/M7Cj Ttt4Fqd0J4XDOd6JPZt35VTMqbHgHjTJct/6PkP3wfyCCbanPimpxoblVzCerIFz2apC 63APKGjiTXZXnHGKzkxLJAYFDM+aq1FoHy0w8BHH5WtFQCRB3yqBTtSlVPndWA+itSsU VqWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=vEc1REouCHskATqJzYSjFWUiQy/G3hP9LkDdu0HAEDI=; b=0Op3gjH+DZUR9IRCzFZ/nIxJQZFCSzVHJ9pLkRkbmyY7BRhAFeWXengMi0AT662xjZ 3ViIda+88+msuqniXfflbh/gld7M5eb59dB5jd9JN15JBxH7v64SzjkGhF7MHg6dNfwp V9KHG1kIhPUuYvHlORodMl2jzPUvdL62NpA5uXonUSpJY9vY4FdIhhUksqKhoGX+wLFl uw0VNlVyr1BqzM2iLvF8ksP6lzAPTg0h6McajiAzcJsEoTILeInuEdA/X3bfu13W3v/G fTAHLPiYPGUPgNW7t19zrflm2xuqgWUdDr9L7fN3DKVVW19kBN0RM02OIGAHLAH2yufs ykNw== X-Gm-Message-State: AOAM530oimzQH+1cHNe5G8WDR39mj+PU/wLyuptVJ2Fb4Sj/Cmz6YTl6 E1cslNaL6QNcYaGoZ9BrKZivU+ipy9/wG238oSKYWAKRUrJMUneL X-Google-Smtp-Source: ABdhPJz8kCYzFaGSuMZ+5XdMm78IdjDgEQ/OPAdF2dPth9GNKTvJJz82yi5xnPwN+lHuMBcjNjJh9tSq8r0wU/6RT68= X-Received: by 2002:a05:620a:d56:: with SMTP id o22mr24652315qkl.753.1638665451624; Sat, 04 Dec 2021 16:50:51 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Sun, 5 Dec 2021 07:50:41 +0700 Message-ID: To: Hassan Ahmed <7snovic@gmail.com> Cc: php internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] RFC [Discussion] array_column results grouping From: the.liquid.metal@gmail.com (Hendra Gunawan) > > Hello, this is a proposal to upgrade the functionality of > `array_column` to return an indexed array for all of the matched data > instead of a single result. > > the RFC had been created here > https://wiki.php.net/rfc/array_column_results_grouping and the PR is > in here https://github.com/php/php-src/pull/7698. > > However, I am not sure, if we need to add a fourth parameter to the > function to change its behavior or we can add it as a new function, > something like `array_column_group` or any other name. > Hai Hassan. It seems that your proposal is just save no more than 3 lines compared with `foreach`. Moreover, `foreach` can do better than your version. foreach ($persons as $p) { $groups[$p["education"]][$p["sex"]][MORE_SUBGRUP][] = $p; } output: $groups = [ "elementary" => [ "female" => [ MORE_SUBGRUP_1 => [ 0 => [...], 1 => [...] ], MORE_SUBGRUP_2 => [ 0 => [...], 1 => [...] ], ... ], "male" => [ MORE_SUBGRUP_1 => [ 0 => [...], 1 => [...] ], MORE_SUBGRUP_2 => [ 0 => [...], 1 => [...] ], ... ], ], "highschool" => [ ... ], ... ]; my suggestions are: - don't limit to array => involve object as well - offer reducer callable as an optional last param. reducer callable it will transform 2 steps algo (populating the groups and then reducing) to become a single step. but if it is not provided, the function returns the member of the group (same as example above). old way: // step-1 $group = []; foreach ($persons as $p) {/*...*/} // step-2 foreach ($group as &$eduGroup) { foreach ($eduGroup as &$sexGroup) { $sumWeight = 0; foreach ($sexGroup as $item) { $sumWeight += $item->weight; } $sexGroup = $sumWeight / count($sexGroup); } } output: $groups = [ "elementary" => [ "female" => 40, "male" => 50, ], "highschool" => [ "female" => 60, "male" => 65, ], ... ]; new way: array_column_group( $group, ["education", "sex", MORE_SUBGRUP], fn ($item, $i, $cumm) => ($item->weight + ($cumm * $i)) / ($i + 1) ); the signature: // array_column_group signature array_column_group( array|object $array, int|string|array $index_key, callable $reducer = null ) // callable signature fn (array|object $item, int|string $index, mixed $cummulative): mixed note: - $index_key can be in the form `["education", ...]` or `"education"`. - or even `[0, 1, ...]` or `0`. So virtually, `array_column_group` is a hybrid between `array_column` and `array_walk` (or other function with a funny name which I don't remember). Best Regards Hendra Gunawan.