PHP natsort() example

This tutorial shows an example how to sort an array using natsort() function available in PHP. The natsort() function sorts an array using natural order sorting algorithm.

This function implements a sorting algorithm that orders alphanumeric strings in the way a human being would read while maintaining key/value associations.

Create a php webpage called natsort.php with the below source code.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>PHP asort() example</title>
    </head>
    <body>
        <?php
        $array1 = array("img22.png", "img20.png", "img4.png", "img2.png");
        echo 'Before sorting => ';
        print_r($array1);
        
        echo "<br/>";
        
        asort($array1);
        
        echo 'After sorting => ';
        print_r($array1);
        ?>
    </body>
</html>

If you run the above file then you will get output as shown below

php natsort

Thanks for reading.

Author: Soumitra

Hi, I am, Soumitra, the owner of roytuts.com and it is my passion for sharing my knowledge and I have been writing blogs on various technologies since 2014. If you like my tutorials, you may also want to like my Facebook Page, follow me on Twitter, Github.

Leave a Reply

Your email address will not be published. Required fields are marked *