Doc #81692 [Com]: array_unique bug

[email protected] ("stanislav dot eismont at gmail dot com") Fri, 03 Dec 2021 15:47:50 +0000
Newsgroups php.doc.bugs
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=81692&edit=1

 ID:                 81692
 Comment by:         stanislav dot eismont at gmail dot com
 Reported by:        stanislav dot eismont at gmail dot com
 Summary:            array_unique bug
 Status:             Verified
 Type:               Documentation Problem
 Package:            Arrays related
 Operating System:   ubuntu 20.04
 PHP Version:        7.4.26
 Block user comment: N
 Private report:     N

 New Comment:

To me it isn't look like a doc related problem. I gave such a big array as an example for a reason. If you change this array somehow, let's say remove last element, then there will be one "red" value printed https://3v4l.org/tpjMt.


Previous Comments:
------------------------------------------------------------------------
[2021-12-03 15:30:54] [email protected]

> Isn't this just a result of inconsistent comparisons during
> sorting?

Right, although PHP 5 produced the required results[1].

Anyhow, SORT_REGULAR has a fundamental flaw when working on
arbitrarly mixed scalar types, namely that the result is not
necessarily in monotonic order, i.e. the sort has arbitrary
results (depending on the sorting algorithm).  E.g. consider ['2',
'a', 1]; the following holds prior to PHP 8.0.0:

    '2' < 'a'
    'a' < 1
    '2' > 1

As of PHP 8.0.0, this is no longer the case for the given example
(thanks to the saner string to number comparisons), but consider
another example: ['!', '0', true]; the following holds:

    '!' < '0'
    '0' < true
    '!' == true

That still doesn't work.

So the current O(log n) algorithm of array_unique(), would need to
be changed to an O(n²) algorithm, or the sorting algorithm would
need to be changed back to what we had in PHP 5.  Neither option
is desireable, and I think we should just document this
limitation.  (And we also should fix the note in the description
section, which only applies for SORT_STRING.)

[1] <https://3v4l.org/RfFJr>

------------------------------------------------------------------------
[2021-12-03 15:13:58] [email protected]

Isn't this just a result of inconsistent comparisons during sorting? The sorted arrays don't place the two reds next to each other with SORT_REGULAR; they do if you use SORT_STRING, which also produces the expected unique array.
https://3v4l.org/JTQMr
https://3v4l.org/gPaPn

------------------------------------------------------------------------
[2021-12-03 12:25:04] [email protected]

Indeed, broken as of PHP 7.0.0: <https://3v4l.org/PXQZe>.

------------------------------------------------------------------------
[2021-12-03 11:30:02] stanislav dot eismont at gmail dot com

Description:
------------
array_unique returns incorrect result with SORT_REGULAR flag.

Test script:
---------------
<?php
$array = ["red", "color" => "red", 78, 78, 12, "12", false, null, false, 0, true, 1, 0, null, 2.12, "2.12", 8, "-s"];
var_dump(array_unique($array, SORT_REGULAR));


Expected result:
----------------
Expected one "red" value printed:


array(11) {
  [0]=>
  string(3) "red"

// ...

}

Actual result:
--------------
Actually "red" printed twice:

array(11) {
  [0]=>
  string(3) "red"
  ["color"]=>
  string(3) "red"

// ...

}


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=81692&edit=1