-
Notifications
You must be signed in to change notification settings - Fork 736
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
array_unique with SORT_REGULAR flag does not always work as expected #1463
Comments
array_unique performs a sort and that means it's susceptible to the SORT_REGULAR problem (as documented in sort):
Here, the "mixed types" aren't strings versus numbers but strings vs numeric strings. Because PHP compares numeric strings as if they were numbers. |
See also https://bugs.php.net/bug.php?id=81692. |
This is the point, I didn't realize about this. Maybe it make sense to put this sentence in the array_unique SORT_REGULAR flag description, for me it was not clear, I expected strings to be sorted always as strings, even if they are numeric strings. If it doesnt make sense for you to add this to the documentation, please feel free to close my issue. Thanks a lot! |
It's not just about array_unique: the same thing happens when you do regular comparisons like with Comparisons of numeric strings are covered in another area of the manual (but you don't know that looking at array_unique). |
Ok, then I will close this issue. Thanks for your support! |
There's a legitimate concern here: a developer using array_unique (and a couple other array functions) don't benefit from the same SORT_REGULAR disclaimer that the sort page includes. The documentation should get some kind of note/warning to match - and the warning possibly even improved to say a little more than just "unexpected results". |
Yes, the point is this one, the disclaimer warns about unexpected results in mixed types. For example, the bug sent by @cmb69 makes sense because there are mixed types in the array. The problem for me is that numeric strings is not a type. It is a string or it is not. In my example, my array contains only strings, therefore I expected to be sorted in the same way as SORT_STRING flag. Let's keep it open then, maybe a small detailed warning about the flag make sense. |
Description
The following code:
Resulted in this output:
But I expected this output instead:
It happens because of the value "15p100", if I remove the letter "p" it works. It happens with any character not digit (example "15+100").
PHP Version
PHP 8.1.2
Operating System
No response
The text was updated successfully, but these errors were encountered: