Bug #75559 [NEW]: array_unique() with SORT_REGULAR misbehaves with array of objects
From: geompse at gmail dot com
Operating system: Debian
PHP version: 7.2.0RC6
Package: Arrays related
Bug Type: Bug
Bug description:array_unique() with SORT_REGULAR misbehaves with array of objects
Description:
------------
According to the documentation, array_unique() does the following:
(string) $elem1 === (string) $elem2
When passed the SORT_REGULAR flag, the comparaison is done without
changing types:
$elem1 === $elem2
In a particuliar case maybe related to the length of the array, the
unicity is not done, and duplicate objects subsists.
In the test script below, you can see that $a !== $b because $a is an
instance of class A, and $b is an instance of class B
When adding multiple times the same object ($a) in an array, the
array_unique/SORT_REGULAR does function properly with only one occurence
kept.
When adding a different object ($b) the array_unique/SORT_REGULAR does
not function properly, returning an array containing duplicates.
According to 3v4l.org the issue is reproductible in almost every
versions of PHP ranging between 5.2.9 - 5.6.30 and 7.0.0 - 7.2.0rc6:
https://3v4l.org/fYtvR (only versions 5.6 & 7)
https://3v4l.org/sE03m (most versions)
On my side I will deprecate array_unique by adding it to the
"disable_functions" directive, however I would really like to see this
issue resolved.
Thank you.
Test script:
---------------
<?php
class A {};
$a = new A();
class B {};
$b = new B();
$array = array();
for($i=0; $i<28; $i++)
$array[$i] = $a;
$array[0] = $array[26] = $b;
# var_dump($array);
$array = array_unique($array,SORT_REGULAR);
var_dump($array);
Expected result:
----------------
array(5) {
[0]=>
object(B)#2 (0) {
}
[1]=>
object(A)#1 (0) {
}
}
Actual result:
--------------
array(5) {
[0]=>
object(B)#2 (0) {
}
[1]=>
object(A)#1 (0) {
}
[2]=>
object(A)#1 (0) {
}
[13]=>
object(A)#1 (0) {
}
[26]=>
object(B)#2 (0) {
}
}
--
Edit bug report at https://bugs.php.net/bug.php?id=75559&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=75559&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=75559&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=75559&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=75559&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=75559&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=75559&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=75559&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=75559&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=75559&r=support
Expected behavior: https://bugs.php.net/fix.php?id=75559&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=75559&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=75559&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=75559&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=75559&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=75559&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=75559&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=75559&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=75559&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=75559&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=75559&r=mysqlcfg
Thread (1 message)
- geompse at gmail dot com