Edit report at http://bugs.php.net/bug.php?id=54571&edit=1
ID: 54571 User updated by: [email protected] Reported by: [email protected] Summary: asort is not stable (mixes keys) Status: Bogus -Type: Bug +Type: Feature/Change Request Package: Arrays related Operating System: any PHP Version: 5.3.6 Block user comment: N Private report: N New Comment: Ok. Since this behavior seems to be documented, but there are other functions with "preserve_key" parameters, i'd like to make this a feature request. Previous Comments: ------------------------------------------------------------------------ [2011-04-20 01:56:45] danvarin at googlemail dot com Ok. Since this behavior seems to be documented, but there are other functions with "preserve_key" parameters, i'd like to make this a feature request. ------------------------------------------------------------------------ [2011-04-20 01:32:36] [email protected] Known, documented, and not a bug. [1] http://www.php.net/manual/en/array.sorting.php "If any of these sort functions evaluates two members as equal then the order is undefined (the sorting is not stable)." [2] http://lxr.php.net/opengrok/xref/PHP_5_3/ext/standard/array.c#490 [3] http://lxr.php.net/opengrok/xref/PHP_5_3/Zend/zend_qsort.c#56 ------------------------------------------------------------------------ [2011-04-20 00:12:49] [email protected] Description: ------------ In the manual it sais: "This function sorts an array such that array indices maintain their correlation with the array elements they are associated with." But when we have the same value the order of the keys is swapped. (See Test Script). This is unexpected and kind of destroys the correlation. In computer science terms one would say asort is not a stable sorting function. Tested with: - PHP 5.3.3-7+squeeze1 with Suhosin-Patch - PHP 5.3.6 - PHP 5.3.5 - PHP 5.2.17 Test script: --------------- <?php $testarray = array ('1' => (int) 23, '2' => (int) 1, '4' => (int) 1); var_dump($testarray); asort($testarray); var_dump($testarray); //4 and 2 are switched ?> Expected result: ---------------- array(3) { [1]=> int(23) [2]=> int(1) [4]=> int(1) } array(3) { [2]=> int(1) [4]=> int(1) [1]=> int(23) } Actual result: -------------- array(3) { [1]=> int(23) [2]=> int(1) [4]=> int(1) } array(3) { [4]=> int(1) [2]=> int(1) [1]=> int(23) } ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=54571&edit=1
