Description
Let's say we want to validate an array with SemVer numbers:
$array = [
"14.0",
"14.1",
"14.10",
];
These are unique, and array_unique
agrees with me:
array_unique($array);
Results in:
[
"14.0",
"14.1",
"14.10",
]
But the unique rule in this package uses an additional flag, called SORT_REGULAR. According to the PHP docs:
SORT_REGULAR - compare items normally (don't change types)
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.