php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #65208 array_unique SORT_STRICT flag for predictable results with type-mixed values
Submitted: 2013-07-05 10:02 UTC Modified: 2013-07-05 10:04 UTC
Votes:3
Avg. Score:4.7 ± 0.5
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:2 (66.7%)
From: of dot olivier dot favre at gmail dot com Assigned:
Status: Open Package: Arrays related
PHP Version: 5.4.4 OS: Debian wheezy
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: of dot olivier dot favre at gmail dot com
New email:
PHP Version: OS:

 

 [2013-07-05 10:02 UTC] 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,
)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-07-05 10:04 UTC] of dot olivier dot favre at gmail dot com
-Summary: array_unique with SORT_REGULAR inconsistently compare 1 and "1" +Summary: array_unique SORT_STRICT flag for predictable results with type-mixed values -Operating System: +Operating System: Debian wheezy -PHP Version: 5.4.17 +PHP Version: 5.4.4
 [2013-07-05 10:04 UTC] of dot olivier dot favre at gmail dot com
Better patch summary.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Mon Mar 25 16:01:26 2019 UTC