search for in the  
<krsortlist>
Last updated: Thu, 19 May 2005

ksort

(PHP 3, PHP 4, PHP 5)

ksort -- Sort an array by key

Description

bool ksort ( array &array [, int sort_flags] )

Sorts an array by key, maintaining key to data correlations. This is useful mainly for associative arrays.

Returns TRUE on success or FALSE on failure.

Example 1. ksort() example

<?php
$fruits
= array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
ksort($fruits);
reset($fruits);
while (list(
$key, $val) = each($fruits)) {
   echo
"$key = $val\n";
}
?>

The above example will output:

a = orange
b = banana
c = apple
d = lemon

You may modify the behavior of the sort using the optional parameter sort_flags, for details see sort().

See also asort(), arsort(), krsort(), uksort(), sort(), natsort(), and rsort().

Note: The second parameter was added in PHP 4.



User Contributed Notes
ksort
justin at booleangate dot org
18-Jan-2005 03:04
Here's a handy function for natural order sorting on keys.

function natksort($array) {
  // Like ksort but uses natural sort instead
  $keys = array_keys($array);
  natsort($keys);

  foreach ($keys as $k)
   $new_array[$k] = $array[$k];

  return $new_array;
}
yaroukh at email dot cz
06-May-2004 10:08
I believe documentation should mention which of array-functions do reset the internal pointer; this one does so ...
pedromartinez at alquimiapaginas dot com
28-Nov-2003 09:58
A list of directories can be listed sorted by date (newer first) with this script. This is usefull if the directories contain (for example) pictures and you want the newer to appear first.

$maindir = "." ;
$mydir = opendir($maindir) ;

// SORT
$directorios = array();
while (false !== ($fn = readdir($mydir)))
{
   if (is_dir($fn) && $fn != "." && $fn != "..")
   {
       $directory = getcwd()."/$fn";
       $key = date("Y\-m\-d\-His ", filectime($directory));
       $directorios[$key] = $directory;
   }
}

ksort($directorios);
$cronosdir = array();
$cronosdir = array_reverse($directorios);

while (list($key, $directory) = each($cronosdir)) {
   echo "$key = $directory<bR>";
}

Pedro
09-Mar-2002 09:09
here 2 functions to ksort/uksort an array and all its member arrays

function tksort(&$array)
  {
  ksort($array);
  foreach(array_keys($array) as $k)
   {
   if(gettype($array[$k])=="array")
     {
     tksort($array[$k]);
     }
   }
  }

function utksort(&$array, $function)
  {
  uksort($array, $function);
  foreach(array_keys($array) as $k)
   {
   if(gettype($array[$k])=="array")
     {
     utksort($array[$k], $function);
     }
   }
  }
delvach at mail dot com
06-Nov-2001 03:29
A real quick way to do a case-insensitive sort of an array keyed by strings:

uksort($myArray, "strnatcasecmp");
sbarnum at mac dot com
19-Oct-2001 05:54
ksort on an array with negative integers as keys yields some odd results.  Not sure if this is a bad idea (negative key values) or what.

<krsortlist>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT