Re: RFC proposal for adding SORT_STRICT flag to array_unique()
| From: | Valentin Udaltsov | Date: | Fri, 24 Oct 2025 22:23:19 +0000 |
| Subject: | Re: RFC proposal for adding SORT_STRICT flag to array_unique() | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-128956@lists.php.net to get a copy of this message | ||
сб, 25 окт. 2025 г. в 01:18, Morgan <Weedpacket@varteg.nz>:
> 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 = [];
> foreach($source_array as $a) {
> if(!in_array($a, $uniques, true)) {
> $uniques[] = $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<T>,
callable(T, T): -1|0|1): array<T>`.
--
Valentin