English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP ksort() Function Usage and Example

PHP Array Function Manual

The ksort() function sorts the array by key name

Syntax

ksort ( $array, $sort_flag );

Definition and Usage

The ksort() function sorts the array by key while preserving the association between key names and data. This function is mainly used for associative arrays.

Parameter

Serial NumberParameters and Description
1

array

Required. Specify an array

2

sort_flag

Optional. Specify how to sort the array values. Possible values-

  • SORT_REGULAR - Default value. Treat the value as is (do not change the type)

  • SORT_NUMERIC - Process the value numerically

  • SORT_STRING - Treat the value as a string

  • SORT_LOCALE_STRING - Treat the value as a string according to the local settings

Return Value

This function returns TRUE on success and FALSE on failure.

Online Example

<?php
   $transport = array( 'a'=>'foot', 'b'=>'bike', 'c'=>'car', 'd'=>'plane');
   ksort($transport);
   
   print_r($transport);
?>
Test and see‹/›

Output Result:

Array ( [a] => foot [b] => bike [c] => car [d] => plane )

  PHP Array Function Manual