Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: ksort - Manual
[go: Go Back, main page]

PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

list" width="11" height="7"/> <krsort
Last updated: Thu, 31 May 2007

view this page in

ksort

(PHP 4, PHP 5)

ksort — 配列をキーでソートする

説明

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

キーとデータの関係を維持しつつ、配列をキーでソートします。 この関数は、主として連想配列において有用です。

成功した場合に TRUE を、失敗した場合に FALSE を返します。

例 307. ksort() の例

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

上の例の出力は以下となります。


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

    

オプションのパラメータ sort_flags によりソートの動作を修正可能です。詳細については、 sort() を参照ください。

asort()arsort()krsort()uksort()sort()natsort() および rsort() も参照ください。

注意: 二番目のパラメータは、PHP 4 で追加されました。



list" width="11" height="7"/> <krsort
Last updated: Thu, 31 May 2007
 
add a note add a note User Contributed Notes
ksort
maxx at opentvnet dot hu
21-Aug-2007 10:17
Beware! The function removes the keys with an empty value from the array. :-(
don dot hosek at gmail dot com
25-May-2007 10:37
It's worth noting that ksort is also handy if you want to be able to do manipulations on an numerically-keyed array which may not have been initialized in numerical order:

$a[2]=7;
$a[0]=1;
$a[1]=6;

foreach($a as $b) {
  echo "$b ";
}

$a = ksort($a);

foreach($a as $b) {
  echo "$b ";
}
06-Nov-2006 10:26
Why not just use built-in PHP functions? You can do an in-place natural sort by keys with:

uksort($array, 'strnatcasecmp');
richard dot quadling at bandvulc dot co dot uk
24-Oct-2005 05:10
Just to complete the comments made by ssb45.

If the supplied array is an empty array, the value returned is NOT an array.

All that is required is to pre-initialize the result.

function natksort(&$aToBeSorted)
    {
    $aResult = array();
    $aKeys = array_keys($aToBeSorted);
    natcasesort($aKeys);
    foreach ($aKeys as $sKey)
        {
        $aResult[$sKey] = $aToBeSorted[$sKey];
        }
    $aToBeSorted = $aResult;
    return True;
    }
ssb45 at cornell dot edu
30-Jun-2005 08:58
The function that justin at booleangate dot org provides works well, but be aware that it is not a drop-in replacement for ksort as is.  While ksort sorts the array by reference and returns a status boolean, natksort returns the sorted array, leaving the original untouched.  Thus, you must use this syntax:

$array = natksort($array);

If you want to use the more natural syntax:

$status = natksort($array);

Then use this modified version:

function natksort(&$array) {
    $keys = array_keys($array);
    natcasesort($keys);

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

    $array = $new_array;
    return true;
}
justin at booleangate dot org
18-Jan-2005 06: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
07-May-2004 12:08
I believe documentation should mention which of array-functions do reset the internal pointer; this one does so ...
pedromartinez at alquimiapaginas dot com
29-Nov-2003 12: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
10-Mar-2002 12: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
07-Nov-2001 06: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
20-Oct-2001 07: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.

list" width="11" height="7"/> <krsort
Last updated: Thu, 31 May 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites