How to use natsort in PHP

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

natsort() function in PHP

  • The natsort() function is used to sort an array by using a "natural order" algorithm.
  • The natsort() function is maintaining key/value associations
  • The natsort() function returns TRUE on success.
  • The natsort() function returns FALSE on failure.

Syntax

natsort(array)

Parameter

  • array array is required parameter. it is the input array to sort.

Example

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

<html>

<body>

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

    <?php

    $array1 = array('code1'.'<br/>', 'code10'.'<br/>', 'code5'.'<br/>',

                'code9'.'<br/>', 'code3'.'<br/>', 'code15'.'<br/>');

    $array2 = $array1;

    sort($array1);

    echo "Standard sorting <br />";

    print_r($array1);

    natsort($array2);

    echo "<br />Natural order sorting<br />";

    print_r($array2);

    ?>

</body>

</html>

 

Output

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