From:             
Operating system: any 
PHP version:      5.3.6
Package:          Arrays related
Bug Type:         Bug
Bug description:asort is not stable (mixes keys)

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 bug report at http://bugs.php.net/bug.php?id=54571&edit=1
-- 
Try a snapshot (PHP 5.2):            
http://bugs.php.net/fix.php?id=54571&r=trysnapshot52
Try a snapshot (PHP 5.3):            
http://bugs.php.net/fix.php?id=54571&r=trysnapshot53
Try a snapshot (trunk):              
http://bugs.php.net/fix.php?id=54571&r=trysnapshottrunk
Fixed in SVN:                        
http://bugs.php.net/fix.php?id=54571&r=fixed
Fixed in SVN and need be documented: 
http://bugs.php.net/fix.php?id=54571&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=54571&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=54571&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=54571&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=54571&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=54571&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=54571&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=54571&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=54571&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=54571&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=54571&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=54571&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=54571&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=54571&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=54571&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=54571&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=54571&r=mysqlcfg

Reply via email to