#47370 [Com]: array_unique has backward compatibility problem, and SORT_REGULAR is confusing

From: Date: Thu, 30 Apr 2009 09:46:33 +0000
Subject: #47370 [Com]: array_unique has backward compatibility problem, and SORT_REGULAR is confusing
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-2180@lists.php.net to get a copy of this message
ID: 47370 Comment by: nospam at ez dot no Reported By: for-bugs at hnw dot jp Status: Assigned Bug Type: Documentation problem Operating System: * PHP Version: 5.2.9 Assigned To: andrei New Comment: Hi Guys, We are facing the same BC problem with array_unique. Consider following test script: <?php $array = array( '400.00', '400' ); // Here 400 value exists // array(2) { [0]=> string(6) "400.00" [1]=> string(3) "400" } var_dump($array); $arrayTest1 = array_unique( $array ); // Here 400 value is missing // array(1) { [0]=> string(6) "400.00" } // Prior verstion 5.2.9 this always returned array(2) { [0]=> string(6) "400.00" [1]=> string(3) "400" } var_dump($arrayTest1); $arrayTest2 = array_unique( $array, SORT_STRING ); // Here 400 value exists // array(2) { [0]=> string(6) "400.00" [1]=> string(3) "400" } var_dump( $arrayTest2 ); ?> This is definitely BC break in 5.2.9 as comparing '400.000' and '400' in array_unique in PHP versions prior 5.2.9 returned both values. In PHP 5.2.9 it return '400.000'. Previous Comments: ------------------------------------------------------------------------ [2009-04-30 08:26:29] jani@php.net See also bug #48115 (yes, WTF?!) ------------------------------------------------------------------------ [2009-03-01 07:03:12] moriyoshi@php.net Andrei, you must add a note about the behavioral change. ------------------------------------------------------------------------ [2009-03-01 00:51:32] for-bugs at hnw dot jp Hi, Andrei. Here's another terrible example. <?php $a=array("10","1az", "1e1"); var_dump(array_unique($a)); $b=array("1e1","10", "1az"); var_dump(array_unique($b)); The result is: array(3) { [0]=> string(2) "10" [1]=> string(3) "1az" [2]=> string(3) "1e1" } array(2) { [0]=> string(3) "1e1" [2]=> string(3) "1az" } The array $a and $b have same 3 elements with different ordering. Although, two array_unique() returns different result. First array_unique() returns 3 elements in spite of the fact that "10" equals "1e1" with ==. In fact, the two arrays are both sorted about SORT_REGULAR. Because "10" < "1az" , "1az" < "1e1" and "1e1"=="10". Sorting with SORT_REGULAR is not stable, and unique element is not always in neighbor. This behavior is not obvious for almost all PHP programmer. You should explain the detail of your function in reference manual. ------------------------------------------------------------------------ [2009-02-14 08:28:18] for-bugs at hnw dot jp OK, you think comparing elements as string is harmful, doesn't you? Then, how about array_diff() or array_intersect()? They compare array elements with string representation. Isn't it harmful? ------------------------------------------------------------------------ [2009-02-14 08:22:26] moriyoshi@php.net This was not discussed, and thus no bogus thing. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/47370 -- Edit this bug report at http://bugs.php.net/?id=47370&edit=1

« previous php.doc.bugs (#2180) next »