PHP ksort() Function

PHP ksort() Function is used to sort associative array in ascending order according to the key. An ksort() function is built-in function in PHP.

Syntax :

ksort(array $array , $sort_type = SORT_REGULAR);

Parameter,

$array : Required. It is input array.

$sort_type : Optional. $sort_type may be used to modify the sorting behavior using below values.

  1. SORT_REGULAR - Default. Compare items normally (don't change types).
  2. SORT_NUMERIC - Compare items numerically.
  3. SORT_STRING - Compare items as strings.
  4. SORT_LOCALE_STRING - Compare items as strings that based on current locale.
  5. SORT_NATURAL - Compare items as strings using natural ordering.
  6. SORT_FLAG_CASE -  It can be combined with SORT_STRING or SORT_NATURAL to sort strings case-insensitively.

 Return Values :

It returns true on success or false on failure. A ksort() Function sorts an associative array in ascending order according to the key.

Let's see below example to understand php ksort() Function in details.

Example :

<?php
$age= array("jacky"=>37,"nile"=>47,"dirgh"=>25,"walker"=>56,"arya"=>"15");
echo "<br><br>A ksort() Function sorts an associative array according to key in ascending order<br>";
ksort($age);
foreach($age as $key => $value)
{
echo "<br/> <b>Key :</b> $key ; <b>Value :</b> $value";
}
?>

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

17945