PHP Array ksort() Function

What is ksort() Function?

Explanation

The "ksort()" function can sort an array by keys. Values have their original keys intact.

Syntax:


ksort(array,sorttype)

In the above syntax "array" is the array to be sorted using the keys,"sorttype" specifies the type in which to be sorted, for example "SORT_REGULAR",is the default type, "SORT_NUMERIC", treat values numerically, "SORT_STRING", treat values as strings, "SORT_LOCALE_STRING", treat values as string based on local settings.

Example :


<?php
$b=array("1"=>"Grapes","2"=>"Apple","3" =>"Cherry");
ksort($b);
print_r($b);
?>
Result :

[1] => Grapes
[2] => Apple
[3] => Cherry

In the above example the array "$b" is sorted based on the keys, displays values starting from "Grapes".

PHP Topics


Ask Questions

Ask Question