ksort() function in PHP || PHP ksort function
×


ksort() function in php

0 656

PHP ksort() function is used to sort an associative PHP array in ascending order. It is an inbuilt function of PHP. This function sorts the array according to the keys.

Syntax

ksort($array,$type_of_sorting);

here,

1) $array is a PHP array which we want to sort. It is mandatory.

2) $type_of_sorting is a numeric value that decides the type of comparing of array elements. It is optional. It has 6 possible values that specify 6 possible sorting orders.

  • SORT_REGULAR: If a value is 0  then the function compares array elements normally.
  • SORT_NUMERIC: If a value is 1, then the function compares array elements as a numeric value.
  • SORT_STRING: If a value is 2, then the function compares array elements as a string.
  • SORT_LOCALE_STRING: If a value is 3, then this function compares array elements as a string according to the current locale.
  • SORT_NATURAL: If a value is 4, then the function compares array elements as a string with the use of natural ordering.
  • SORT_FLAG_CASE: If a value is 5, then this function compares array elements according to value.

Example:

<?php
$arr=array("RAM"=>"22","SHYAM"=>"1","AJAY"=>"11","RADHA"=>"150","PARI"=>"15");
ksort($arr);
echo "<pre>";
print_r($arr);
?>

Output:



Share:


Leave a Reply


Comments
    Waiting for your comments