Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116009 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 23552 invoked from network); 8 Sep 2021 13:33:24 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 8 Sep 2021 13:33:24 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3D5EA180003 for ; Wed, 8 Sep 2021 07:11:04 -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.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE 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-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (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 ; Wed, 8 Sep 2021 07:11:03 -0700 (PDT) Received: by mail-pl1-f169.google.com with SMTP id n4so1382293plh.9 for ; Wed, 08 Sep 2021 07:11:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech-net.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=5DbD5oh94W1R3LGRjgaeTQNyJ+x7rEciStEZeli3bNs=; b=me+sZ55CIWrUvMGu7N1mchVUomChC0OjdWgU+EhKLfR0JltrXrWQ7mZRZ/QuDG8y6i KJkkPrIW+1VuuL1pVtWw5shFhw77Dht1iJOUAKY+OD6AN4E7Nv/f5eQ9pOued6PJVDU/ GM2jfkqaeR6H65j/kP+YtvHs4c4knwTN1KE4yhCdVIrfwe232lT/BZGyvyOvpAGkhNLp pbIuK2DLmYyxa4feJvjUuIKmUafgwU0v6RIuVm9Xpqrvl0uThI/1bpyA173IxAf8LSk0 nl4ykyrGI09CecEK+RvrG3IzCN642KfqKExVslTugtby+W1KLSQRpYJ8T2OSYrCaHkTu zWFQ== 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:content-transfer-encoding; bh=5DbD5oh94W1R3LGRjgaeTQNyJ+x7rEciStEZeli3bNs=; b=dMMLeWlxc39vnSd7LSRDAb2+1rvAnGURwidY+thUIzTk0U025HcASAChZCSukXITIr e61PmvuW/X6BLRiEKgbFFBTieR0H1Vua8Cs3RxBHeu/+Us97yQTv8nf+5pl+/A2zC1uC qozqXxlS/86dH/LcAr0NRy8gJuwR3UL5JGb13H8DRFtRM7XROD4SzfFCBgm6HCMKBIbh bcHrnpdFbpXnVorkcQ7/oLoHkI3vDuX8Txd2AA9cGGdcbRmAZAq2Z84pCaNfkeWwBcul 998VBtVjMyh3w9I+736gSqx5XlUoCZH7tAZrNCVMs0HpRFiadSQlaKsj72WF9mCm1pgd 9sGg== X-Gm-Message-State: AOAM533KeW0OqQjVgPelKjk8MvXRdaXOCxekcCe32ccV2uAdKQs1VAXY 7Pg3noKYtvEfnKGrMljND/Vf9aYlAMWe82imLrFcoQ== X-Google-Smtp-Source: ABdhPJxD0ln/ICbGkeP2uKJCaZ7/X4V2wZG0Y1qMhRW7jssaZr4JB2PLmAmGJBllG4YzBIwwH1b0fDh6jqhMLgcWfuI= X-Received: by 2002:a17:90a:194a:: with SMTP id 10mr4272581pjh.221.1631110261227; Wed, 08 Sep 2021 07:11:01 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 8 Sep 2021 16:10:50 +0200 Message-ID: To: Marco Pivetta Cc: PHP internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Option for array_column() to preserve keys. From: andreas@dqxtech.net (Andreas Hennings) Thanks for the feedback so far! On Wed, 8 Sept 2021 at 10:13, Marco Pivetta wrote: > > Heyo, > > On Wed, 8 Sep 2021, 02:19 Andreas Hennings, wrote: >> >> Hello internals, >> >> The function array_column() would be much more useful if there was an >> option to preserve the original array keys. >> I can create an RFC, but I think it is better to first discuss the optio= ns. > > > New function, please =F0=9F=99=8F I am not opposed. But I am also curious what others think. What I don't like so much is how the situation with two different functions will have a "historically grown wtf" smell about it. But this is perhaps preferable to BC breaks or overly "magic" parameters or overly crowded signatures. If we go for a new function: A name could be array_column_assoc(). array_column_assoc(array $array, string $value_key) This would behave the same as array_column($array, $value_key), but preserve original keys. Items which are not arrays or which lack the key will be omitted. A $value_key =3D=3D=3D NULL would be useless, because this would simply return the original array. The question is, should it do anything beyond the most obvious? Or should we leave it minimal for now, with the potential for additional parameters in the future? Limitations: If some items are omitted, it will be awkward to restore the missing items while preserving the order of the array. Possible ideas for additional functionality: - Replicate a lot of the behavior of array_column(), e.g. with an optional $index_key parameter. This would be mostly redundant. - Additional functionality for nested arrays? - Fallback value for entries that don't have the key? Or perhaps even a fallback callback like with array_map()? - Option to capture missing entries e.g. in a by-reference variable? A benefit of keeping the limited functionality would be that programming errors are revealed more easily due to the strict signature. A question is how we would look at this long term: Do we want both functions to co-exist long-term, or do we want to deprecate one of them at some point? If array_column() is going to stay, then array_column_assoc() only needs to cover the few use cases that are missing. -- Andreas