|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2017-11-23 15:12 UTC] requinix@php.net
-Type: Bug
+Type: Documentation Problem
[2017-11-23 15:12 UTC] requinix@php.net
[2017-11-23 15:36 UTC] geompse at gmail dot com
[2017-11-23 16:35 UTC] requinix@php.net
[2023-06-20 05:34 UTC] goodasdeoka at gmail dot com
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 18:00:01 2025 UTC |
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) { } }