php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #3718 sort() and rsort() kill array keys
Submitted: 2000-03-03 05:35 UTC Modified: 2000-03-05 11:43 UTC
From: stephan dot beal at compaq dot com Assigned:
Status: Closed Package: Misbehaving function
PHP Version: 3.0.15 OS: Linux 2.2.x with PHP 4B3
Private report: No CVE-ID: None
 [2000-03-03 05:35 UTC] stephan dot beal at compaq dot com
# PHP4B3. Haven't tried PHP3.

$ar = array( "653434" => "Foo", "6452323" => "Bar", "9873" => "foobar" );
while( list( $k, $v ) = each( $ar ) ) {
  echo "$k --- $v<BR>\n";
}
reset( $ar );
sort( $ar );
while( list( $k, $v ) = each( $ar ) ) {
  echo "$k --- $v<BR>\n";  
}

# The first one prints okay.
# In the second one, the keys are reset to 0-2.
# ksort does not do this (obviously), but there
# is no rksort(), and I need to sort a list of files by
# modification time (newest first). Suggestion?
# I currently hold the files as an array, with the 
# filemtime() as the key. Also tried the names as the key,
# and the filemtime() as the value, but then I lose the
# key during a sort() or rsort(), anyway.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-03-05 11:43 UTC] sas at cvs dot php dot net
To maintain index association you need to use asort/arsort. See http://www.php.net/manual/function.asort.php3
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Tue Oct 13 21:00:01 2026 UTC