|
|
PHP Array ksort() Function
|
Tutorials » Php »
|
Topic |
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".
|
|
A Note |
Learn PHP programming language tutorial with simple and neat example. Hope you enjoy this free tutorial.
Do give us your valuable feedback and suggestions on this online tutorial. This is a Copyright Content.
|
|
|
|