PHP ksort() 函数

实例

按照键名对关联数组进行升序排序:

<?php
$age=array("Bill"=>"60","Steve"=>"56","mark"=>"31");
ksort($age);
?>

运行实例

定义和用法

ksort() 函数对关联数组按照键名进行升序排序。

Tip:Please use krsort() The function sorts the associated array in descending order by key name.

Tip:Please use asort() The function sorts the associated array in ascending order by key value.

Syntax

ksort(array,sortingtype);
Parameters Description
array Required. Specify the array to be sorted.
sortingtype

Optional. Specify how to arrange the elements/items of the array. Possible values:

  • 0 = SORT_REGULAR - Default. Arrange each item in the standard order (Standard ASCII, do not change type).
  • 1 = SORT_NUMERIC - Treat each item as a number.
  • 2 = SORT_STRING - Treat each item as a string.
  • 3 = SORT_LOCALE_STRING - Treat each item as a string, based on the current locale setting (can be changed by setlocale()).
  • 4 = SORT_NATURAL - Treat each item as a string, using a natural sorting similar to natsort().
  • 5 = SORT_FLAG_CASE - Can combine (bitwise OR) SORT_STRING or SORT_NATURAL to sort strings without case sensitivity.

Description

The ksort() function sorts an array by key name, retaining the original keys of the array values.

The optional second parameter contains additional sorting flags.

Returns TRUE if successful, otherwise returns FALSE.

Technical Details

Return Value: Returns TRUE if successful, returns FALSE if failed.
PHP Version: 4+