You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So based on that it makes sense to use the flag. But unfortunately, it doesn't...
array_unique($array, SORT_REGULAR):
Results in:
[
"14.0",
"14.1",
]
As you can see 14.1 and 14.10 are considered the same. That's because SORT_REGULAR triggers a loose comparison, see: https://stackoverflow.com/a/14803087.
With loose comparison:
"14.1" == "14.10";
Results in true, but with strict comparison:
"14.1" === "14.10"
Results in false.
That's why I think we shouldn't use the SORT_REGULAR flag, but just the regular array_unique. Please let me know if there are some other considerations.
There are multiple solutions:
Provide the flag as parameter and default to SORT_REGULAR flag.
Drop the SORT_REGULAR flag.
Something else I can't think of right now 🤔
I'm willing to create a PR for this, so just let me know.
The text was updated successfully, but these errors were encountered:
dvdheiden
changed the title
Unique rule should not use the SORT_REGULAR flag
Unique rule should not use the SORT_REGULAR flag by default
Mar 27, 2025
dvdheiden
changed the title
Unique rule should not use the SORT_REGULAR flag by default
Unique rule has some issue with SORT_REGULAR flag
Mar 27, 2025
dvdheiden
changed the title
Unique rule has some issue with SORT_REGULAR flag
Unique rule has an issue with SORT_REGULAR flag
Mar 27, 2025
I totally agree with you that we have a bug here. I'm inclined to DROP the flag altogether, unless there's a good reason not to. If you would be up for creating a pull request, I would be happy to review and merge it!
Let's say we want to validate an array with SemVer numbers:
These are unique, and
array_unique
agrees with me:Results in:
But the unique rule in this package uses an additional flag, called SORT_REGULAR. According to the PHP docs:
So based on that it makes sense to use the flag. But unfortunately, it doesn't...
Results in:
As you can see
14.1
and14.10
are considered the same. That's because SORT_REGULAR triggers a loose comparison, see: https://stackoverflow.com/a/14803087.With loose comparison:
Results in
true
, but with strict comparison:Results in
false
.That's why I think we shouldn't use the SORT_REGULAR flag, but just the regular
array_unique
. Please let me know if there are some other considerations.There are multiple solutions:
I'm willing to create a PR for this, so just let me know.
The text was updated successfully, but these errors were encountered: