Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128956 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 3C17A1A00BC for ; Fri, 24 Oct 2025 22:23:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1761344621; bh=3YMVvaUP4ocFwfPXx+RZEVw1r8A5rAjCkAwaYjA/rus=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=IVNqzUqvTzJuur9rDKPpWwKL4K1NrniTuCbHXMkLyBzSzccTmbCFA4hmkljdFrgE1 t7rpo5DLGpknmTwURjmKu+F2Vzm33rMgQz+Jzo6gzGxtWygJk10eAqD8rFJPZxxxLQ qEAEoFdO3XqmHtQXPXrXIIc9YOqmp7NoTZr10VlKynwttGCHUFoswygHDjiJaTASVi 2g+nuWDORP8mxEjUGIEc7l6amxfMRFHUyWeSksVbVvAQPnzCpmRwGbKmd9SsA83tPa uB/xrAsGJm1S/QnjRV4QbJvaDqn/kgwnBzizcwZ/VT+BqT47FxXCzIvFu6By0lZFmY T4zlE0Q6ARkzw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id DE64C180056 for ; Fri, 24 Oct 2025 22:23:40 +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.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_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-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (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 22:23:37 +0000 (UTC) Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-47112edf9f7so14758705e9.0 for ; Fri, 24 Oct 2025 15:23:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1761344611; x=1761949411; 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=j+Bq0diTK3ScEi8DLOSQYrWbulPh3h5n7J5dWwUF5Rk=; b=hCnqS6vrzwutWAaaWQM61C8hPaHhWiUq/Rj46T6YAxyzJM4XSzwFBLYvsdGO3gqAfi uQHj3I1JLHxFwl9a3E05TIb3jPD59Oa3m6xAUk5gUTgNiizHgjZw0OSASbDBolSqNmK3 HX3JCmFctffLEewpJQop8VgTNCW3Od0Ic06QpSXa6f+b4T7xoaCLuhhljPxFcIqhRxPJ lFzAfJmXpjwOnaIhqoWxEacOryZK/g2DuT9bNJ5zZKkDLx4/Z4qwEfCg434iGCu1A8ug Pdhehefq6iZ2nhwM4xCqAjDBBmHFq8QIcxGMuN5vXdbjCc3NANJCyXe3/zDWU5ThcbM5 t7UA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761344611; x=1761949411; 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=j+Bq0diTK3ScEi8DLOSQYrWbulPh3h5n7J5dWwUF5Rk=; b=EqHgvuNRyEPYLj/DjSyupJjWgLPIpyumiU07NxmC2afqxiFEt5j9pEhpfxMKAE/EmR B0Jathk0WiS/WuEp8gimX9xLtjvmFAwhjXpricwK8XcvxJmHGKtj84lurleeivxQHWd+ z4xnQ/2Y/Z3yXKiQ9o3q7ORYTrHaKs0pCxXz6nRXTBTQFw4cCZSmS0o5Ds5S6O505XqZ 599x4ausfCKTRh0ScVxAQPw9zg8wBZKY5jnxBEbpH0X/3ZpaW6EN8hD2kmipcJpLsonM ROJaRf+dWNCqbHTbbl7tu7cxa+clDoupLkU08mekwnSdXkbML4QUDK0vSeIbE5EjBT1y xetA== X-Gm-Message-State: AOJu0YwuvwX+xoJ0snOLI18VxqLuFhPbU/GgYzLBryYQyB9//IKa33tL Fo6uHwsfE49WLIKRBY6DtOwjOPuV5HQt3Lzjkg4J/u4FFr+KxNh4D5h5yWFjnvXuBaL3OZmy9pa QV/M1Mhnz8UqZUxAL3JyJng1DrkvDzSLrzGk6Tr39 X-Gm-Gg: ASbGnctAb+chjRRpxjBL6sKPFEpihynuwERGq0PxvdWymyUOicpDZ+R9ebzyXr6Qo7Q 40MPyLC0vTi7nRZBlSrs3NpG4CLMthT08o3lL20te6KwPRilYbXZL6jZ5Kx/LhwVrvoCFPTpvWG SRvRKXfbJdUNEg158xxLCS1W8hxFdKnWgV6lhdZtcsNktCmGB2I2R6XQfNex2egcY1qDooph3pC D7h32WV3JPaOaugwwPYmurcAM49wSkSItKqpHCQAjd5U7qjfRrGXcIJDI4aHqX1RByIsg== X-Google-Smtp-Source: AGHT+IH9rJWruYQ/Z66H2S8DlIHa9sKqtilWW69hhNJl88M9n8HejmNCHjko+THOsudDc73AQ3YvOWFXo2d3F16qhV0= X-Received: by 2002:a05:600c:4686:b0:46f:b340:75e7 with SMTP id 5b1f17b1804b1-475caf92ae1mr62710395e9.8.1761344611194; Fri, 24 Oct 2025 15:23:31 -0700 (PDT) Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 References: <4b605609-47c2-4df5-be71-8b962ad9ff1e@varteg.nz> In-Reply-To: <4b605609-47c2-4df5-be71-8b962ad9ff1e@varteg.nz> Date: Sat, 25 Oct 2025 01:23:19 +0300 X-Gm-Features: AWmQ_blBsuzXiWA96JybBTiK26SImDKbC53TW3n4OPIIGwwqXuYGwvs31vyG3RI Message-ID: Subject: Re: [PHP-DEV] RFC proposal for adding SORT_STRICT flag to array_unique() To: Morgan Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000e09d510641eeff51" From: udaltsov.valentin@gmail.com (Valentin Udaltsov) --000000000000e09d510641eeff51 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable =D1=81=D0=B1, 25 =D0=BE=D0=BA=D1=82. 2025=E2=80=AF=D0=B3. =D0=B2 01:18, Mor= gan : > On 2025-10-25 08:34, Jason Marble wrote: > > Hello everybody! > > > > > > 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 know I find array_unique generally useless due to its insistence on > stringifying everything for comparison. > > ``` > $uniques =3D []; > foreach($source_array as $a) { > if(!in_array($a, $uniques, true)) { > $uniques[] =3D $a; > } > } > ``` > > I seem to recall part of the issue is that array_unique works by sorting > its elements so that "equal" values are adjacent. I know this would be > done on O(n log(n)) vs. O(n^2) grounds, but that could be addressed at > least in part by a smarter sort criterion that sorts by type/class (in > some arbitrary order) before sorting by value. For uncomparable types > (i.e., instances of most classes) this would be by object ID, because we > don't _actually_ care about ordering. > I would rather propose smth like usort: `array_uunique(array, callable(T, T): -1|0|1): array`. --=20 Valentin --000000000000e09d510641eeff51 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
=D1=81=D0=B1, 25 =D0=BE=D0=BA=D1=82. 2025= =E2=80=AF=D0=B3. =D0=B2 01:18, Morgan <Weedpacket@varteg.nz>:
On 2025-1= 0-25 08:34, Jason Marble wrote:
> Hello everybody!
>
>
> The potential for a `SORT_NATURAL` flag also came to mind as another <= br> > useful addition, but I believe `SORT_STRICT` is the more critical
> feature to discuss first.
>

I know I find array_unique generally useless due to its insistence on
stringifying everything for comparison.

```
$uniques =3D [];
foreach($source_array as $a) {
=C2=A0 =C2=A0 =C2=A0if(!in_array($a, $uniques, true)) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0$uniques[] =3D $a;
=C2=A0 =C2=A0 =C2=A0}
}
```

I seem to recall part of the issue is that array_unique works by sorting its elements so that "equal" values are adjacent. I know this wou= ld be
done on O(n log(n)) vs. O(n^2) grounds, but that could be addressed at
least in part by a smarter sort criterion that sorts by type/class (in
some arbitrary order) before sorting by value. For uncomparable types
(i.e., instances of most classes) this would be by object ID, because we don't _actually_ care about ordering.

I would rather pr= opose smth like usort: `array_uunique(array<T>, callable(T, T): -1|0|= 1): array<T>`.

--
Valentin
--000000000000e09d510641eeff51--