Skip to content
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

Open
davidjr82 opened this issue Mar 15, 2022 · 7 comments
Open

Comments

@davidjr82
Copy link

Description

The following code:

<?php
print_r(array_unique(["100","100","25","15p100","25","25"], SORT_REGULAR));

Resulted in this output:

Array
(
    [0] => 100
    [2] => 25
    [3] => 15p100
    [4] => 25
)

But I expected this output instead:

Array
(
    [0] => 100
    [2] => 25
    [3] => 15p100
)

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

@davidjr82 davidjr82 added bug Documentation contains incorrect information Status: Needs Triage labels Mar 15, 2022
@damianwadley
Copy link
Member

array_unique performs a sort and that means it's susceptible to the SORT_REGULAR problem (as documented in sort):

Warning

Be careful when sorting arrays with mixed types values because sort() can produce unexpected results, if flags is SORT_REGULAR.

Here, the "mixed types" aren't strings versus numbers but strings vs numeric strings. Because PHP compares numeric strings as if they were numbers.

@damianwadley damianwadley transferred this issue from php/php-src Mar 15, 2022
@damianwadley damianwadley removed bug Documentation contains incorrect information Status: Needs Triage labels Mar 15, 2022
@cmb69
Copy link
Member

cmb69 commented Mar 16, 2022

See also https://bugs.php.net/bug.php?id=81692.

@davidjr82
Copy link
Author

... PHP compares numeric strings as if they were numbers.

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!

@damianwadley
Copy link
Member

Maybe it make sense to put this sentence in the array_unique SORT_REGULAR flag description, for me it was not clear,

It's not just about array_unique: the same thing happens when you do regular comparisons like with < or >.
https://3v4l.org/I2vjm

Comparisons of numeric strings are covered in another area of the manual (but you don't know that looking at array_unique).
https://www.php.net/manual/en/language.operators.comparison.php
https://www.php.net/manual/en/language.types.numeric-strings.php

@davidjr82
Copy link
Author

Ok, then I will close this issue. Thanks for your support!

@damianwadley
Copy link
Member

damianwadley commented Mar 16, 2022

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".

@davidjr82
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants