# array_unique() and object comparison

- Thread: https://externals.io/message/68499
- Original archive: https://news-web.php.net/php.internals/68499
- Started by Matthieu Napoli on 2013-08-13T10:15:12+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #68499 — Matthieu Napoli — 2013-08-13T10:15:12+00:00

URL: https://externals.io/message/68499#68499

array_unique() is perfect for removing duplicates in a string array, but 
not so great for an array of objects.

 `SORT_REGULAR` was added to compare items without casting them to string. 
However it uses the "==" comparison operator.

I see two improvements:

- on a short term, improve the documentation to make it explicit that 
the "==" operator will be used (the example with "(string) $elem1 === 
(string) $elem2" is misleading, "===" should be replaced with "==")

- on a long term, maybe add another option SORT_STRICT (like the $strict 
parameter for in_array): that sort would use the strict comparison 
operator "==="

Any thoughts about that?

Matthieu

---

## #68521 — Tjerk Anne Meesters — 2013-08-15T10:49:43+00:00

URL: https://externals.io/message/68499#68521 · In reply to: #68499

On Tue, Aug 13, 2013 at 6:15 PM, Matthieu Napoli <matthieu@mnapoli.fr>wrote:

> `array_unique()` is perfect for removing duplicates in a string array, but
> not so great for an array of objects.
>
> `SORT_REGULAR` was added to compare items without casting them to string.
> However it uses the "==" comparison operator.
>

Unless you have inserted those objects in the array using an identity map,
comparisons with == would be the most useful imho.


>
> I see two improvements:
>
> - on a short term, improve the documentation to make it explicit that the
> "==" operator will be used (the example with "(string) $elem1 === (string)
> $elem2" is misleading, "===" should be replaced with "==")
>

In fact, this is the behaviour if `SORT_STRING` is given, so perhaps moving
that note to where the flags are described makes sense.


>
> - on a long term, maybe add another option SORT_STRICT (like the $strict
> parameter for in_array): that sort would use the strict comparison operator
> "==="
>

+1 for that. In fact, I wouldn't mind having the option to use a custom
comparison function (though this function gets called for both the sorting
and pruning process).

:)


> Any thoughts about that?
>
> Matthieu
>
> --
> 
> 
>
>


-- 
--
Tjerk
