PHP Functions For Sorting Array In Ascending Order: sort().
The sort() function sorts an indexed array in ascending order.
Sort the elements of the $MyArray array in ascending numerical order:
<?php $MyArray=array(8, 20, 4, 100, 50, 6, 9, 11, 22, 5); sort($MyArray); print_r($MyArray); ?>
Output:
Array ( [0] => 4 [1] => 5 [2] => 6 [3] => 8 [4] => 9 [5] => 11 [6] => 20 [7] => 22 [8] => 50 [9] => 100 )
Sort the elements of the $MyArray array in ascending alphabetical order:
<?php $MyArray=array("PHP", "HTML", "CSS", "JavaScript", "Mysql", "Java", "AngularJs", "WordPress"); sort($MyArray); print_r($MyArray); ?>
Output:
Array ( [0] => AngularJs [1] => CSS [2] => HTML [3] => Java [4] => JavaScript [5] => Mysql [6] => PHP [7] => WordPress )
My name is Mukesh Jakhar and I am a Web Application Developer and Software Developer, currently living in Jaipur, India. I have a Master of Computer Application in Computer Science from JNU Jaipur University. I loves to write on technology and programming topics. Apart from this, I love to travel and enjoy the beauty of nature.