-
Notifications
You must be signed in to change notification settings - Fork 7.9k
array_unique: add way to compare items with an identity check (===
)
#10526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is the expected behaviour, as From the
Therefore, changing this to a feature request, by possibly introducing a new flag. |
SORT_REGULAR
flag===
)
I just wonder what the behaviour should be in case someone decides to use this new flag in sorting and the values aren't comparable because they are of different types. An exception maybe? But then again that's probably a bit strange because array_unique wouldn't have this issue. |
Should the default behaviour of |
|
New sorting method have to sort by type first, because Thus new method would look something like this: function cmp_strict(mixed $a, mixed $b): int
{
if ($a === $b) return 0;
$typeA = gettype($a);
$typeB = gettype($b);
if ($typeA === $typeB) {
// abstract function to compare values without type juggling
return cmp_values_strict($a, $b);
} else {
return $typeA <=> $typeB;
}
}
As with sort functions, |
Thanks for pointing that out, it looks indeed like a logical comparison function. I can spend some time this week to try and implement a PoC. |
Err wait... The example code of a comparator posted here wouldn't work too... For example The main problem is that there seems to be no total order relation on elements of all sorts of different type combinations. For example: is '0'<0, or is 0<'0'? If both answers are yes then we have an inconsistency in sorting. If both answers are no then they must be equal, which causes OP's problem. In other cases the ordering is arbitrary. So although this issue proposes to add a new flag, we cannot use that flag in a consistent way for sorting. The flag would be unique to array_unique and must be handled in a special way AFAICT. Or we have to come up with an ordering scheme that works for all type combinations. |
@nielsdos Branch with spaceship operator is only comparing types, which are always string and always starting with letter, so it's safe comparison. https://3v4l.org/jnLno
"integer" < "string" thus 0<'0'.
Sorting by type first is consistent way of sorting, not sure if should be allowed in sort, but won't hurt anything.
Comparing types first should be faster for mixed arrays, which is ideal for This is a bit closer to proper implementation: https://3v4l.org/2oRQt |
@KapitanOczywisty Sorry, my bad, I misread the code. So the solution here is the "arbitrary order" option I listed which seems not super arbitrary after all since it seems to be consistent with |
… identity check (===) Implements phpGH-10526. This adds a SORT_STRICT flag to use in sorting and in array_unique, although it is most useful in the latter case. If the types are equal we will use the identity check first before using zend_compare to avoid possibly inconsistent results (e.g. null and false are equal according to zend_compare). If the types are equal, but the values are not identical we can rely on zend_compare to perform the comparison without introducing inconsistencies. If the types are not equal, we will sort in a way that groups the values of the same type together, and within those groups the values are sorted. We determine the order of the types based on the alphabetical order (as exposed to userland!), which is consistent with the inequality comparisons of `zend_compare` (e.g. false < object < true etc.) Note that double comes before long because the name exposed to userland is float which comes alphabetically before long.
This is essentially the same as #9775, so I'm closing this as a duplicate. Please continue discussion there if you have anything to add. |
Uh oh!
There was an error while loading. Please reload this page.
Description
The following code:
Resulted in this output:
But I expected this output instead:
Adding the
SORT_REGULAR
flag should result in items being compared normally (not changing item types).PHP Version
PHP 8.0.20
Operating System
No response
The text was updated successfully, but these errors were encountered: