How to use ksort in PHP

In this article, I will explain how the ksort() function can be used in PHP.
  • 1617

ksort() function in PHP

  • The ksort() function is used to sort an array by the keys in reverse order. The values keep their original keys.

  • The ksort() function returns TRUE on success, or FALSE on failure.

  • The ksort() function is useful mainly for associative arrays.

Syntax

ksort(array,sorttype)

Parameter

  • array array is required parameter. the specified array to be sort.
  • sorttype sorttype is optional parameter. it determine, how to sort the array values. Possible values of sorttype is
    • SORT_REGULAR - this is the default case value. it don't change types.
    • SORT_NUMERIC - it's treat values numerically.
    • SORT_STRING - it's treat values as strings.
    • SORT_LOCALE_STRING - it's treat values as strings, based on local settings.

Example

The following example show to how the ksort() function can be used in PHP.

<html>

<body>

<h3 style="color: violet;">ksort() function example in PHP</h3>

    <?php

    $my_demo = array("b" => "Student".'<br/>', "c"=> "Teacher".'<br/>', "a" => "Advocate".'<br/>');

    ksort($my_demo);

    print_r($my_demo);

    ?>

</body>

</html>

 

Output

ksortphp.jpg

You may also want to read these related articles here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.