How to use sort in PHP

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

sort() function in PHP

  • The sort() function is used to sort an array by the values.
  • In sort() function, elements will be arranged from lowest to highest.
  • The sort() function assigns new keys for the elements in the array.
  • In sort() function, Existing keys will be removed.
  • The sort() function returns TRUE on success, or FALSE on failure.

Syntax

sort(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 sort() function can be used in PHP.

 <html>

<body>

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

    <?php

    $my_demo = array("a" => "Nitin".'<br/>', "b" => "Manish".'<br/>', "c" =>      

    "Arvind".'<br/>');

    sort($my_demo);

    print_r($my_demo);

    ?>

</body>

</html>

 

Output

sortphp.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

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.