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.

王林
Release: 2023-09-08 14:26:02
forward
1161 people have browsed it

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)
Copy after login

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.

< h2>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>";
}
?>
Copy after login

Output

headphone = 10
mouse = 24
pendrive = 13
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template