Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:124451 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 4F6B71A00B7 for ; Wed, 17 Jul 2024 00:29:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1721176278; bh=tZiNaIHx/rJUENr3IkvATPpgpokC5X2Ak6b0F/5cwWU=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=ASIMMTwPBqNUI4lT0rDXBjshJprdXbc/n/4IRDDpzItCy6Ok1f8j5KDPBwEZuVQP4 JC4ejmj0fbrBZNEJ2l/YJ+zcjRoODBmWCWvhOXIIgOH5qiL3o5tjOC/1TiGJUukBO3 CYFqtok0P9amvdN01g24apLtdtdZ6orDKrHT1Bw4qH7HsbwNLHZkow/SHV84edtJ/u XZvF+YjrNIfxzOVbTGA0I9ZCPDonDhXdMPLpMaGkqjdAu5EpX49DB8LBnTA9FYCnoF pXjPJ5oxpyqCWwz6FviMDb3E00pHfUadL7IRFxgOoJ+Mpy/KRkbRvqjKbkw5fmvdwn 0Pfu5iqHqXnyA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id F2F2D180056 for ; Wed, 17 Jul 2024 00:31:17 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-ej1-f45.google.com (mail-ej1-f45.google.com [209.85.218.45]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 17 Jul 2024 00:31:17 +0000 (UTC) Received: by mail-ej1-f45.google.com with SMTP id a640c23a62f3a-a77c2d89af8so687423566b.2 for ; Tue, 16 Jul 2024 17:29:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721176186; x=1721780986; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=tZiNaIHx/rJUENr3IkvATPpgpokC5X2Ak6b0F/5cwWU=; b=W5XpQSmLtJCBQkLrwv6sMyzorjc7MmsVM0veAjybUI9i+2aPqicNIjenczXavzi7nw WLhrgZEv36W1YSziBjSkB33fvydSVFjzFUoS1vqkob61LSw7vGqxtiOet0XKFSutAHpi H+LwXbBHLoJ9r+nhlBfEMHHKFQwXk2FbkkMegS9333E2k+qdds3bdfJnTK59z4UpqRkY 9C17iHIMTcLL1WgBjhwUOadxCXuLGMP/zb9+RYGREYgVLOHv418J7+RMJdDjCQgXLMkX 5/+ZrEoZxfajzk6BzFLz1BjeBWZIw3zMDMV3G8YQFlkQHLc4SDGGmGsUPcaVXrmwbJFz ROIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721176186; x=1721780986; 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=tZiNaIHx/rJUENr3IkvATPpgpokC5X2Ak6b0F/5cwWU=; b=vY197Q27s5AwhfFcTZ3EVDwfvSvCy4cAnl3CVJBlSAZ9Ez0Roh2DeRmLafSvjgXDIx 5UcZOiYF9nK+kUAiooOCne8GZcQ9KMHW9zUtZwSOCUdcDGMBu8NTyf3Fj0fYzZRnGREQ abuM9/E3RsBZ26NRR0mNglocs0IcyhJd0vlh3qgzSsxHJ9GNO1nPxGQyHdF/ph3kmjUN r0QcVMHg3JU2KThXdmzzug++b7UrqkoqU0ZilGcguzv2veppdXh/McB9RqDws2ejN5KL CDpf8rXtpgAW6M0T+8Bpgx7lZG2x9KG4wpNpjPrJtGvgIlzUm/N/yEwS5tS77FyIPPTd gm6w== X-Gm-Message-State: AOJu0YyT8ct2HmSLSf977kbo7ezz4GxjcZ0Fu7Cle9B0EIZ6G4jeMg0m QynLZm0yPgCaDgzjk65wtapeZiMw7/AyADXGnN2dmEHmuLSrJ67oeT52RTwNlH92Kbb4tYCfE9P JOsYYb6Io6nkMU7yMyvrczPDQk8U2Z0VO X-Google-Smtp-Source: AGHT+IE3jO/z2DRDyE38PtHx7iehb8BFs/Y9wi/qafkugvpVU8uKhMmAoH16DGY6FAobj2FQ0UuATK4gK94s+5V408c= X-Received: by 2002:a17:906:ca4d:b0:a77:ae4b:7bf6 with SMTP id a640c23a62f3a-a7a0133779bmr3433966b.54.1721176186076; Tue, 16 Jul 2024 17:29:46 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: <5bdbffef-4954-415d-9dae-4c9bd4ba0f22@scriptfusion.com> In-Reply-To: <5bdbffef-4954-415d-9dae-4c9bd4ba0f22@scriptfusion.com> Date: Wed, 17 Jul 2024 10:29:34 +1000 Message-ID: Subject: Re: [PHP-DEV] array_reduce callback key To: Bilge Cc: PHP Internals List Content-Type: multipart/alternative; boundary="0000000000002ac3bc061d668faa" From: mickmackusa@gmail.com (mickmackusa) --0000000000002ac3bc061d668faa Content-Type: text/plain; charset="UTF-8" This is not the first time that a developer needs access to the input array's keys within the callback of array_reduce(). It is untrue that you "can't access it"; you just need to use an array of keys as the input array. Some frameworks already have the feature of key access built into their methods/functions. https://laravel.com/docs/11.x/collections#method-reduce Examples of passing array_keys to array_reduce (some of which are more than a decade old): https://stackoverflow.com/a/10196070/2943403 https://stackoverflow.com/a/27419768/2943403 https://stackoverflow.com/a/27809675/2943403 https://stackoverflow.com/a/32226816/2943403 https://stackoverflow.com/a/33372066/2943403 https://stackoverflow.com/a/37728665/2943403 https://stackoverflow.com/a/39322532/2943403 https://stackoverflow.com/a/45508817/2943403 https://stackoverflow.com/a/48664250/2943403 https://stackoverflow.com/a/49035053/2943403 https://stackoverflow.com/a/57966850/2943403 https://stackoverflow.com/a/58997848/2943403 https://stackoverflow.com/a/73726419/2943403 https://stackoverflow.com/a/76196482/2943403 mickmackusa On Wed, 17 Jul 2024, 09:15 Bilge, wrote: > Hi Internals, > > I just ran into a case where I needed the array key in an array_reduce() > callback, but I can't access it :( > > So I wrote a PR to fix it: https://github.com/php/php-src/pull/14986 > > What do you think? Does this need an RFC? > > Cheers, > Bilge > --0000000000002ac3bc061d668faa Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
This is not the first time that a develo= per needs access to the input array's=C2=A0keys within the callback of = array_reduce().
It is untrue that you=C2=A0"can't access= it"; you just need to use an array of keys as the input array.
<= div>
Some frameworks already have the feature of key access b= uilt into their methods/functions.

Examples of passing arr= ay_keys to array_reduce (some of which are more than a decade old):
https://stackov= erflow.com/a/10196070/2943403

mickmackusa


<= div class=3D"gmail_quote">
On Wed, 17 = Jul 2024, 09:15 Bilge, <bilge@scriptfusion.com> wrote:
Hi Internals,

I just ran into a case where I needed the array key in an array_reduce() callback, but I can't access it :(

So I wrote a PR to fix it: https://github.com/ph= p/php-src/pull/14986

What do you think? Does this need an RFC?

Cheers,
Bilge
--0000000000002ac3bc061d668faa--