Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128954 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 E660A1A00BC for ; Fri, 24 Oct 2025 19:34:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1761334493; bh=3Um3wyfcrD4YqrrU58SiJgBUwkTjjzy1NyvW1XGQbFk=; h=From:Date:Subject:To:From; b=XKB3M1nPVriK/b2fuoFDwLIQI5kFkk5x5cw3rq8NTr9+ymh/viHsyxfzaBKNE9KCD tt+ttTxCHmaLhD3Z+umXCfDwhef4XMikLhpfPojOTOVDnp6JjPCRsS97dmnRf6OJri QKRChiASy8XjiUAkzhvhI+5jvl94fPWNENFXknIrkMQ5sD5PSIkpDOv8Eb3Foz3heV P/eDzESRL2Q7AJtXgw75aRihQ4dtXMgagoP8WFehJuUJP3t3hcrutVgbnLXcW7/TLy xQN/PuGpO6g1nmx0dl575RImv4m/wedKQlysa0nuIMyOqtQjN+lJt9tpo3e5QymrTq pKr9vgAY7c1Bw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C29B6180056 for ; Fri, 24 Oct 2025 19:34:52 +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.7 required=5.0 tests=BAYES_05,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,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: No X-Envelope-From: Received: from mail-yx1-f45.google.com (mail-yx1-f45.google.com [74.125.224.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 ; Fri, 24 Oct 2025 19:34:52 +0000 (UTC) Received: by mail-yx1-f45.google.com with SMTP id 956f58d0204a3-63e3a7a67a4so2782642d50.1 for ; Fri, 24 Oct 2025 12:34:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intuitivetechnology.com; s=google; t=1761334487; x=1761939287; darn=lists.php.net; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=+B/RFio6TK2KMeFr9u/at4LDVwlo100wdMZGcKBtzKo=; b=iuioETKoJ2ccBcmiqMfIunGhaONCR1GQV0dOmNilxEPKMEfMO3AwX3XfrK4SKCmXZT 9qLAX2JvyEU/g2JVQT1Dd+mA9ksImxlVzDIpYgcutcx48BE0nSvpuGdh86jSY4Y+RxqD 4usQ/TPJ04X83xA4kL4qvYTTblj3z7T3oIogSG54zpbndVH7Il8lvSxqDu+M+74ywhau j0IqZfr1LtX2zq2w6SD331kqrEY/OKvMlE4PvmGce6uovLS3pdYatw3W+eNW4ILpdUKA YrCZR1hpAlXZ++bYOyj4MMpLGJjKHjJyENXnCDxJ2T/bUC81ropsg08wH69WQ5cAb73J 865A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761334487; x=1761939287; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=+B/RFio6TK2KMeFr9u/at4LDVwlo100wdMZGcKBtzKo=; b=GRX/F0EEGF30xk5cboO06kpvcNWxEVsMQK6/3xrK9ewHs6Vd6cRBsMSQfRANVB1UVR BB0b3D3Etq7CaJk4XbQO0aMpj5A+2+cXZV8m/zc4ZY+fAHFmXJnj4FVjBVzm+fKYDQ6M c3GNvvqCzGaaFgCXBoMrZEfD3tWvS5+8eGnlDvQWUxfwKA7cLZrLCV02Ek3aFlGNVIhC lRrIvNRJAFWGbcBHrow/K1F1p4kniNV+bvslnRwYB/4hOlhxTcn/L13djSf0BZkgo4YB Fuy3A99GmYzyLiSCbAe6P1cn0xXDTS1aVaFxn/5+bC5Xr+nBk1e/EadjahA2v7H5UBp8 uL/A== X-Gm-Message-State: AOJu0YwDljbJomTZc2MfG6ypkiiENjK8mKqjI6jVusUP7gD/X1gQUQLf 3bl64aLYwUFUsCRTwmYi35/2se+oFihr02K4D2PTMX1iiNR+qPA05Z9euOi5QCFy8NhTrkMvzdG sxbrjRteLhUl6odTouMXYOF5yvs5anNKqh06nluBUyuyLbOG441YuK0s= X-Gm-Gg: ASbGncvb9bm9aJb9eYCKFGHfMkjkzYqBiJDMc1FKVfi+k+OBu9Md420sGfoQ3LMhxKC IBIYh1jsXoJh3Hlja3UcBOfrFHLFCYtna5JKerfWCgSuWYIGKElzGNqpYlKCW73MnzzABf/HJc/ nVMA+B/1R1eARaW0Fii//nybJSaXD6RvnYww7RR1LfW6PoyEsxPat96DqYwvjR4k36DPOu83rRK Ut+1emP1WslooIP7cvA492thrA+BP1ZOcM+dQsF0kvPjj97JHPd3eJFyZn3Cg== X-Google-Smtp-Source: AGHT+IHONCQFYMN+v5bnr5bYouS4GsddQgeFxK9a0bE7fWyGUdbIJ8iMNrg8+BVt8juFZgsuER44So1LPJHLlIPAtXA= X-Received: by 2002:a05:690e:2591:b0:63e:30c0:75e with SMTP id 956f58d0204a3-63f378c6b64mr4786751d50.54.1761334486798; Fri, 24 Oct 2025 12:34:46 -0700 (PDT) Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 Date: Fri, 24 Oct 2025 12:34:36 -0700 X-Gm-Features: AS18NWDSBWqLVow-93-Qu88d_KNEs-gXun3xlrKwaT1qzQ5ahSuf4vDS5oGSU54 Message-ID: Subject: [PHP-DEV] RFC proposal for adding SORT_STRICT flag to array_unique() To: internals@lists.php.net Content-Type: multipart/alternative; boundary="0000000000006aa1930641eca433" From: jmarble@intuitivetechnology.com (Jason Marble) --0000000000006aa1930641eca433 Content-Type: text/plain; charset="UTF-8" Hello everybody! I'd like to open a discussion regarding the behavior of `array_unique()` with the `SORT_REGULAR` flag when used on arrays containing mixed types. Currently, `SORT_REGULAR` uses non-strict comparisons, which can lead to unintentional data loss when values like `100` and `"100"` are treated as duplicates. This forces developers to implement user-land workarounds. Here is a common scenario where this behavior is problematic: ```php $events = [ ['id' => 100, 'type' => 'user.login'], // User event (int) ['id' => "100", 'type' => 'system.migration'], // System event (string) ['id' => 100, 'type' => 'user.login'], // Duplicate user event ]; $event_ids = array_column($events, 'id'); // [100, "100", 100] // Current behavior with SORT_REGULAR $unique_ids = array_unique($event_ids, SORT_REGULAR); // Result: [100] // The string "100" is lost due to type coercion. ``` To address this, I propose adding a new flag, `SORT_STRICT`, which would use strict (`===`) comparisons to differentiate between values of different types. With the new flag, the result would be: ```php // Proposed behavior with SORT_STRICT $unique_ids = array_unique($event_ids, SORT_STRICT); // Result: [100, "100"] // Both integer and string values are preserved. ``` I've already submitted a PR to correct the bug I just highlighted: PR: https://github.com/php/php-src/pull/20273 The potential for a `SORT_NATURAL` flag also came to mind as another useful addition, but I believe `SORT_STRICT` is the more critical feature to discuss first. I look forward to your feedback. Thanks, - Jason --0000000000006aa1930641eca433 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello everybody!

I'd like to open a d= iscussion regarding the behavior of `array_unique()` with the `SORT_REGULAR= ` flag when used on arrays containing mixed types.

Currently, `SORT_= REGULAR` uses non-strict comparisons, which can lead to unintentional data = loss when values like `100` and `"100"` are treated as duplicates= . This forces developers to implement user-land workarounds.

Here is= a common scenario where this behavior is problematic:

```php
$ev= ents =3D [
=C2=A0 =C2=A0 ['id' =3D> 100, 'type' =3D&g= t; 'user.login'],=C2=A0 =C2=A0 =C2=A0 =C2=A0 // User event (int)=C2=A0 =C2=A0 ['id' =3D> "100", 'type' =3D>= ; 'system.migration'],=C2=A0 // System event (string)
=C2=A0 =C2= =A0 ['id' =3D> 100, 'type' =3D> 'user.login']= ,=C2=A0 =C2=A0 =C2=A0 =C2=A0 // Duplicate user event
];

$event_id= s =3D array_column($events, 'id'); // [100, "100", 100]
// Current behavior with SORT_REGULAR
$unique_ids =3D array_unique= ($event_ids, SORT_REGULAR); // Result: [100]
// The string "100&quo= t; is lost due to type coercion.
```

To address this, I propose a= dding a new flag, `SORT_STRICT`, which would use strict (`=3D=3D=3D`) compa= risons to differentiate between values of different types.

With the = new flag, the result would be:

```php
// Proposed behavior with S= ORT_STRICT
$unique_ids =3D array_unique($event_ids, SORT_STRICT); // Res= ult: [100, "100"]
// Both integer and string values are preser= ved.
```

I've already submitted a PR to correct the bug I jus= t highlighted:
PR:=C2=A0https://github.com/php/php-src/pull/20273
The potential for a `SORT_NATURAL` flag also came to mind as a= nother useful addition, but I believe `SORT_STRICT` is the more critical fe= ature to discuss first.

I look forward to your feedback.

Than= ks,=C2=A0=C2=A0
- Jason
<= br>
--0000000000006aa1930641eca433--