Req #65208 [Com]: array_unique SORT_STRICT flag for predictable results with type-mixed values

From: Date: Wed, 11 Nov 2020 11:08:13 +0000
Subject: Req #65208 [Com]: array_unique SORT_STRICT flag for predictable results with type-mixed values
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-230262@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=65208&edit=1

 ID:                 65208
 Comment by:         enno dot woortmann at web dot de
 Reported by:        of dot olivier dot favre at gmail dot com
 Summary:            array_unique SORT_STRICT flag for predictable
                     results with type-mixed values
 Status:             Open
 Type:               Feature/Change Request
 Package:            Arrays related
 Operating System:   Debian wheezy
 PHP Version:        5.4.4
 Block user comment: N
 Private report:     N

 New Comment:

Seems like the result of array_unique changed with the 8.0.0beta1 - rc3.

Test script
-----------
var_export(array_unique(['1', 'a', 1, 9, null], SORT_REGULAR));

Output with 8.0.0beta1 - rc3
array (
  0 => '1',
  1 => 'a',
  3 => 9,
  4 => NULL,
)

Output of all 7.x versions (and the alpha versions, except alpha2 which changed the order of the
elements)
array (
  0 => '1',
  1 => 'a',
  2 => 1,
  3 => 9,
  4 => NULL,
)

compare: https://3v4l.org/WTZe6


Previous Comments:
------------------------------------------------------------------------
[2020-08-13 09:34:45] nikic@php.net

Related To: Bug #79890

------------------------------------------------------------------------
[2013-07-05 10:04:23] of dot olivier dot favre at gmail dot com

Better patch summary.

------------------------------------------------------------------------
[2013-07-05 10:02:18] of dot olivier dot favre at gmail dot com

Description:
------------
The doc claims “SORT_REGULAR - compare items normally (don't change types)”
Hence (int)1 and (string)"1" should both be considered non duplicates.

Current results with SORT_REGULAR are too unpredictable for this function/flag to 
be useful.

See #47370:
“I think it's better for SORT_REGULAR to compare elements by using === instead of 
==.”

Request:
Add a SORT_STRICT flag using strict === comparison between elements. (prevents 
from changing SORT_REGULAR behavior).


Tested on:
Debian GNU/Linux 7.1 (wheezy)
$ php --version
PHP 5.4.4-10 (cli) (built: Nov 24 2012 11:21:26) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
    with Xdebug v2.2.0, Copyright (c) 2002-2012, by Derick Rethans

Test script:
---------------
var_export(array_unique(array( 1, 2, 2, 3, "1"), SORT_REGULAR)); // "1" is
suppressed!
echo "\n";
var_export(array_unique(array( 1, 2, 2, 3, "1", "c"), SORT_REGULAR)); //
"1" and "c" are kept (as it should be)
echo "\n";

// Weirder, with the same values in an other order
var_export(array_unique(array( 1, 2, 2, 3, "1", "c"), SORT_REGULAR)); //
"1" and "c" are kept (as it should be)
echo "\n";
var_export(array_unique(array( "c", 1, 2, 2, 3, "1"), SORT_REGULAR)); //
"1" is suppressed!
echo "\n";

Expected result:
----------------
array (
  0 => 1,
  1 => 2,
  3 => 3,
  4 => '1',
)
array (
  0 => 1,
  1 => 2,
  3 => 3,
  4 => '1',
  5 => 'c',
)
array (
  0 => 1,
  1 => 2,
  3 => 3,
  4 => '1',
  5 => 'c',
)
array (
  0 => 'c',
  1 => 1,
  2 => 2,
  4 => 3,
  5 => '1',
)


Actual result:
--------------
array (
  0 => 1,
  1 => 2,
  3 => 3,
)
array (
  0 => 1,
  1 => 2,
  3 => 3,
  4 => '1',
  5 => 'c',
)
array (
  0 => 1,
  1 => 2,
  3 => 3,
  4 => '1',
  5 => 'c',
)
array (
  0 => 'c',
  1 => 1,
  2 => 2,
  4 => 3,
)



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



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


Thread (4 messages)

« previous php.bugs (#230262) next »