Home >Backend Development >PHP Tutorial >In PHP, the function of ksort() function is to sort the array in ascending order by key name.

In PHP, the function of ksort() function is to sort the array in ascending order by key name.

王林
王林forward
2023-09-08 14:25:021125browse

In PHP, the function of ksort() function is to sort the array in ascending order by key name.

ksort() function sorts an array in ascending order by key. Returns TRUE on success and FALSE on failure.

Syntax

ksort(arr, flag)

Parameters

  • arr - The array to sort.

  • Flags -

    • 0 = SORT_REGULAR - Default. Compare items normally. Don't change the type.

    • 1 = SORT_NUMERIC - compares items numerically

    • 2 = SORT_STRING - compares items numerically Compare as strings

    • 3 = SORT_LOCALE_STRING - Compare items as strings based on the current locale

    • 4 = SORT_NATURAL - Compare items as strings using natural ordering.

Return

ksort() function returns TRUE when successful and FALSE when failed.

Example

The following is an example

Live demonstration

<?php
$product = array("headphone"=>"10", "mouse"=>"24", "pendrive"=>"13");
ksort($product);
foreach ($product as $key => $val) {
   echo "$key = $val</p><p>";
}
?>

Output

headphone = 10
mouse = 24
pendrive = 13

The above is the detailed content of In PHP, the function of ksort() function is to sort the array in ascending order by key name.. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete