Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115998 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 43569 invoked from network); 7 Sep 2021 23:41:58 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 7 Sep 2021 23:41:58 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 65AE7180003 for ; Tue, 7 Sep 2021 17: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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,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-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (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 ; Tue, 7 Sep 2021 17:19:31 -0700 (PDT) Received: by mail-pf1-f174.google.com with SMTP id s29so519918pfw.5 for ; Tue, 07 Sep 2021 17:19:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech-net.20150623.gappssmtp.com; s=20150623; h=mime-version:from:date:message-id:subject:to; bh=bxUpkpwijr8MViFcyVogaMZOzDodkQW6nrJdfTupxZ4=; b=IPtVEKD4NDtTkVbXPDDqGJtGlMOAvIn39eRsRImgBX9MYD82ch2oB1xInmhNwv7xUd Gs82aGjBMnO3SD4VURLRZsJ9DtABHvcMEc9hdNJ2Qj6W9OmraC7u0l0ZK3WlR79Q3+g4 6v6dN+aVZxzoQV2r9/vIs2x+/ZXIyKtlIHOkg8yvZeggZ8t8nGVCe4+UaFVAgwstE2/1 Y1XddMogajKP9GJ1tFvibty5YyULySkZX7Gq/7Rw1oKAOsx+9ZGc/08Ma0HCukOrTgu2 82kneMdUVDUCLFKEdKUZAYt0sG2dbFdb9UPyicXqp/IVNIo744bMKhsql9bWyK1XqaUY s8Uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=bxUpkpwijr8MViFcyVogaMZOzDodkQW6nrJdfTupxZ4=; b=c1inA1rRinaXQ+z3qnahRzco0WQiq4XCI0kF3DXMz4jK2ezcwkF0mX2IJS+UQtLenP 5zAAdeMpvljnyYCPuGyCC/EBhsBC5wgakE98xSPT0T8DxGPdujmGPyPRmoTXDq2dTwZg RWygqQpv79pDBIwYRRlmfm5BMDCHJNi/dXs+crrQu/vxAqfhwtYhguEv27TvI1ec6iKe 5fGL6RIdS5hmMaYWbV3Ec/yfmM6sc4WGI5HLQHhcmCC2RFPMBaZFcA2FoVwujJcRkGc7 TfDJlNGRSHnHX5ot27B2An6qCvdvRHrUKWXmcJAu+YnXRV8Qweb/cCFXRSJl07zeotB3 LToA== X-Gm-Message-State: AOAM531gnehv121VG3tiZMM/UfQcMA12oyntZcnRlJO6Tu/+qIODuAF/ 8XoEXTQCyA44SAEE3Ehtp11cjFpKq6Nv/AQs5zbSaKNP1bKkTg== X-Google-Smtp-Source: ABdhPJyDj50hTA3/Uz60ooNDBRxXd5QaidD2zSx7MwHzlN884TtS6EoAyUJ0nT6mMdH+KNtiWf77buBzQAGWRnfpuCE= X-Received: by 2002:a63:d10b:: with SMTP id k11mr978611pgg.26.1631060369271; Tue, 07 Sep 2021 17:19:29 -0700 (PDT) MIME-Version: 1.0 Date: Wed, 8 Sep 2021 02:19:18 +0200 Message-ID: To: PHP internals Content-Type: text/plain; charset="UTF-8" Subject: Option for array_column() to preserve keys. From: andreas@dqxtech.net (Andreas Hennings) 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 options. This is requested in different places on the web, e.g. https://stackoverflow.com/questions/27204590/php-array-column-how-to-keep-the-keys/39298759 A workaround is proposed here and elsewhere, using array_keys() and array_combine() to restore the keys. However, this workaround not only adds complexity, but it breaks down if some items don't have the value key. See https://3v4l.org/im2gZ. A more robust workaround would be array_map(), but this is more complex and probably slower than array_column(), for the given purpose. Some links for your convenience: The function was introduced in this RFC, https://wiki.php.net/rfc/array_column It is now documented here, https://www.php.net/manual/en/function.array-column.php Some ideas how this could be fixed: 1. Allow a magic value (e.g. TRUE) for the $index_key parameter, that would cause the assoc behavior. To fully avoid BC break, this must be a value that previously was completely forbidden. The value TRUE is currently only forbidden with strict_types=1. A value of e.g. new \stdClass is fully forbidden, but would be weird. A constant could be introduced, but this would not prevent the BC concern. 2. Make the function preserve keys if $index_key === NULL. This would be a full BC break. 3. Add an additional parameter with a boolean option or with integer flags. This would be weird, because it would make the $index_key parameter useless. 4. Add a new function. Personally I would prefer option 1, with value TRUE (I can't think of something better). If I could change history, I would prefer option 2. The current behavior could still be achieved with array_values(array_column(..)). Regards, Andreas