Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:127496 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 lists.php.net (Postfix) with ESMTPS id A6BB71A00BC for ; Wed, 28 May 2025 13:42:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1748439601; bh=KPncM6kgMGv00i3aQxpqroY0DGsPLYpS/ySQEGUt9TI=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=K1+ESYQJF5GGJbcAFe+jHs40EpPRSlKXtTwvKLdCp4hRlkA2o0XcyGDoZRZDYic8e yjnfTFhlC7SghbmnZeUbTbtssyv/jD+z9Zbdyy5o/gCs2w67zbQj0ZmCeMTo0PKVjy xMoRMrAiUjTljcgAgO8jFZxpVtmGbOsSlA6VQRECyW5Dkj0Qe9aklwa1AvuSsnt5oO /MKe2IBvStYOmxuVNxwPyINDpwqzbIK2I1qpDNsav9ZydLUjmchNBZbEmLURs+a2xa AvNIDpFr6vj8Jwo5jRewyTipowvhWf8sbxkfcC6simu3uNRIjWvIQQWq799fHxMUaI 47yJxdI4ZqVfg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4ABCC180051 for ; Wed, 28 May 2025 13:40:01 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS, FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from mail-ed1-f50.google.com (mail-ed1-f50.google.com [209.85.208.50]) (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, 28 May 2025 13:40:01 +0000 (UTC) Received: by mail-ed1-f50.google.com with SMTP id 4fb4d7f45d1cf-60410a9c6dcso8441600a12.1 for ; Wed, 28 May 2025 06:42:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1748439726; x=1749044526; 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=+SgnmyZfwCD0gk10D2OHVwEuxH4w7dUoKPGljIRRu3A=; b=Y49wvbgVp675HoPvEpTGiGMLUF4WVCAOHRyn7EaMzjAeJJR/CQshusPnEQorPR24uR tnaNlr/EKEkwmeeOFIb3G7mVdcBGrrzeZyNzG46vZoYxxWDgZ3bBMgeYHBFOswt1e5Qn T7umSYGyPkNZk29ecgdAgY1MOVCCFuU5dGjZcAbgmtUMO7dN06ycFwt++AHhphjckXTi TYQznK0KhAXBWxf4tcALN+k8DmbBt/104LMFqVX/hqRcEbknjRsCwwilDmHcy4OeRPiQ oIkTQ9CAldR8pF3RUQNazT4NW2eee537ShG5pwGpomQnOAPWuNVYs01QwEZsV2D6MH/f Zb1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748439726; x=1749044526; 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=+SgnmyZfwCD0gk10D2OHVwEuxH4w7dUoKPGljIRRu3A=; b=vJkRF597W2F8smLy1Kx+gZEsqjdmBi2Yh/5uPHGCjYlmaTcSxZnuwJuyjj2rRqpcbE I0EDInIzpWmQxHznvw9lGowC79QPFE3e/lyqGlUy3bRi7awFaXvA7uT7CcjTjmBYH0iZ EzvgJ1MNKexAKRUxLt9busU7g7pDtrlWt77BWzVlTcSxPCarZs7JH8sTfLDlWCeyXhzV sonDq7Q26/FQsryXsXI2GsqQZgRqZhKwKfUoMIOJPXz/32ARUqJzkg2z3A3R/tqtGYfZ ROEBq4rFuT4I1qHCsgIv6pEywUFWCZK1g6wsb3wLQ7V7r0VhmUhfO61oG81q8VyESJhf h4uA== X-Gm-Message-State: AOJu0Yzog3iReanahFqj08I4Fk+sEsUxagw5lN8i7wjYYtnkO54CQOCN oBlpbL9YjYT6PH/zlDYd+jy49kgKpnzOOJLsv0tiEqET0sJDGIeV2tIeJZo9GCdcUWpJSAYux4/ wMMYXK8Y0kBJ4KUHRMlbD4vsRDEpy4y/1Mx52 X-Gm-Gg: ASbGncuExshgXr9u0zd3CzaN+4RLC46ZuEnLwimWXXU7hq9QQ8MfHwxNqRs3N6bdpZE WYw5ex4WMWCPR3Gb0XwEsStESqGO3gRQ9jsvOuO/oBFc+W53OeGjWxvTI0CMf6vBVJQFv5/4ytH gGVMOOLw+lgtn5dlKBgmCbZ08CJ+r+HF4AGfQ8IdjHMBHs X-Google-Smtp-Source: AGHT+IEHBicAwxbeuzqbf9mtBS88cZg/Sqrl14iJnUowf79iWORyZAfkR3LRbqU2uHaKLqUm5yZGX89oYqxBWlQhxbI= X-Received: by 2002:a17:907:7255:b0:ad2:4b0c:ee8c with SMTP id a640c23a62f3a-ad85b19f965mr1402557066b.35.1748439725924; Wed, 28 May 2025 06:42:05 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 28 May 2025 21:42:01 +0800 X-Gm-Features: AX0GCFsup66dhIWCkntQMyRVtwPAg1hlp1LG9CuGnPXCe_r-eGAuq8xzEUHjBOs Message-ID: Subject: Re: [PHP-DEV] [RFC] Add preserve_key_types to array_keys() to prevent unintended int conversion To: Daikaras Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000c664d106363258e0" From: a766650827@gmail.com (=?UTF-8?B?6ams5q2j5by6?=) --000000000000c664d106363258e0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable I've carefully read the discussion at [https://externals.io/message/116735]= . While I understand the historical reasons make it difficult to directly change array behavior, this automatic conversion issue does confuse many PHP developers and needs to be addressed. I'd like to propose two solutions= : 1. First, we could add a parameter like preserve_key_types to array functions such as array_keys()/array_search() to temporarily handle the implicit conversion issue. 2. Alternatively, we could introduce a new data type "Map" (inspired by Java and other languages) to completely solve this conversion problem: php =E5=A4=8D=E5=88=B6 // Declaration (preserves original key types)$map =3D new Map(['01' =3D> 'a', '10' =3D> 'b']); // Explicit declaration, keys "01" and "10" won't convert to int$map =3D {"01"=3D>111, "02"=3D>222}; // Syntactic sugar for the above line, similar to array's [] syntax This Map type would support conversion to/from traditional arrays: php =E5=A4=8D=E5=88=B6 $array =3D [1, 2, 3];$map =3D Map::fromArray($array); // Explicit conversio= n $newArray =3D $map->toArray(); // Convert back to traditional array During iteration, keys would maintain their original types: php =E5=A4=8D=E5=88=B6 foreach ($map as $key =3D> $value) { // $key preserves its original type (string/int) } Existing functions like array_keys()/array_search() could accept Map parameters while maintaining all other logic identical to traditional arrays, except keys wouldn't be implicitly converted. [xiaoma] Daikaras =E4=BA=8E2025=E5=B9=B45=E6=9C=8827=E6=97= =A5=E5=91=A8=E4=BA=8C 15:42=E5=86=99=E9=81=93=EF=BC=9A > As Kamil mentioned, this is not limited to a single function but to array > type as a whole. See previous discussion > https://externals.io/message/116735. > > On 5/24/2025 6:43 PM, =E9=A9=AC=E6=AD=A3=E5=BC=BA wrote: > > I propose adding a preserve_key_types parameter to array_keys() to > address issues caused by automatic conversion of string-numeric keys to > integers. > > > --000000000000c664d106363258e0 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

I've caref= ully read the discussion at [https://externals.io/message/116735]. While= I understand the historical reasons make it difficult to directly change a= rray behavior, this automatic conversion issue does confuse many PHP develo= pers and needs to be addressed. I'd like to propose two solutions:

<= ol style=3D"margin:0.5rem 0px 1rem 1.25rem;padding:0px;border:0px;font-vari= ant-numeric:inherit;font-variant-east-asian:inherit;font-variant-alternates= :inherit;font-stretch:inherit;font-size:15px;line-height:inherit;font-size-= adjust:inherit;font-kerning:inherit;font-feature-settings:inherit;font-fami= ly:-apple-system,BlinkMacSystemFont,"Segoe UI","PingFang SC&= quot;,"Microsoft YaHei";vertical-align:baseline;list-style-positi= on:initial;color:rgba(0,0,0,0.9);background-color:rgb(252,252,252)">
  • First, = we could add a parameter like=C2=A0preserve_key_types=C2=A0to array functions such= as=C2=A0array= _keys()/array_search()=C2=A0to temporarily handle the implicit conversion i= ssue.

  • Alternatively, we could introduce a new data type "Map"= (inspired by Java and other languages) to completely solve this conversion= problem:

  • php
    =E5=A4=8D=E5=88=B6
    // Declaration (preserves original key types)
    $map =3D new Map(['01' =3D> 'a', '10' =3D> <=
    span class=3D"gmail-hljs-string" style=3D"margin:0px;padding:0px;border:0px=
    ;font-style:inherit;font-variant:inherit;font-stretch:inherit;font-size:0.8=
    75rem;line-height:1.8;font-size-adjust:inherit;font-kerning:inherit;font-fe=
    ature-settings:inherit;vertical-align:baseline;color:rgb(80,161,79);font-fa=
    mily:inherit">'b']); // Explicit declaratio=
    n, keys "01" and "10" won't convert to int
    $map =3D {"01"=3D=
    >111, "02"=3D><=
    span class=3D"gmail-hljs-number" style=3D"margin:0px;padding:0px;border:0px=
    ;font-style:inherit;font-variant:inherit;font-stretch:inherit;font-size:0.8=
    75rem;line-height:1.8;font-size-adjust:inherit;font-kerning:inherit;font-fe=
    ature-settings:inherit;vertical-align:baseline;color:rgb(152,104,1);font-fa=
    mily:inherit">222}; // Syntactic sugar for the abov=
    e line, similar to array's [] syntax

    This Map type would suppor= t conversion to/from traditional arrays:

    =E5=A4=8D=E5=88=B6
    <= /div>
    <= /div>
    $array =3D [1=
    , 2, 3];
    $map =3D Map::fromArray(// Explicit conversion
    
    $newArray =3D $map->toArray(); During iteration, keys would=
     maintain their original types:

    <= div class=3D"gmail-hyc-common-markdown__code__hd__inner" style=3D"margin:0p= x;padding:0px;border-width:1px 1px 0px;border-style:solid;border-color:rgb(= 224,224,224);font-style:inherit;font-variant:inherit;font-weight:inherit;fo= nt-stretch:inherit;font-size:inherit;line-height:inherit;font-size-adjust:i= nherit;font-kerning:inherit;font-feature-settings:inherit;vertical-align:ba= seline;width:597.486px;height:39.9909px;display:flex;box-sizing:border-box;= border-radius:8px 8px 0px 0px;background:rgb(237,237,237)">
    php
    =E5=A4=8D=E5=88=B6
    foreach ($map as $key =3D> =
    $value) {
        // $key preserves its original type (string/int)
    }

    Existing functions like=C2=A0array_keys()/array_search()=C2=A0could accept Map paramet= ers while maintaining all other logic identical to traditional arrays, exce= pt keys wouldn't be implicitly converted.



    [xia= oma]


    Daikaras <webmaster@daikaras.lt> =E4=BA=8E2025=E5=B9=B45=E6=9C=8827= =E6=97=A5=E5=91=A8=E4=BA=8C 15:42=E5=86=99=E9=81=93=EF=BC=9A
    =20 =20 =20
    As Kamil mentioned, this is not limited to a single function but to array type as a whole. See previous discussion https://externals.io/message/116735.

    On 5/24/2025 6:43 PM, =E9=A9=AC=E6=AD=A3=E5=BC=BA wrote:

    I propose adding a=C2=A0preserve_key_types=C2=A0p= arameter to=C2=A0array_keys()=C2=A0to address issues caused by automatic conversion of string-numeric keys to integers.


    --000000000000c664d106363258e0--