Re: array_unique() and object comparison
| From: | Tjerk Anne Meesters | Date: | Thu, 15 Aug 2013 10:49:43 +0000 |
| Subject: | Re: array_unique() and object comparison | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-68521@lists.php.net to get a copy of this message | ||
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
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
--
Tjerk