doc-en icon indicating copy to clipboard operation
doc-en copied to clipboard

array_unique with SORT_REGULAR flag does not always work as expected

Open davidjr82 opened this issue 2 years ago • 7 comments

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 avatar Mar 15 '22 16:03 davidjr82

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 avatar Mar 15 '22 19:03 damianwadley

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

cmb69 avatar Mar 16 '22 09:03 cmb69

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

davidjr82 avatar Mar 16 '22 10:03 davidjr82

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

damianwadley avatar Mar 16 '22 11:03 damianwadley

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

davidjr82 avatar Mar 16 '22 11:03 davidjr82

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

damianwadley avatar Mar 16 '22 11:03 damianwadley

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.

davidjr82 avatar Mar 16 '22 11:03 davidjr82