Home >>PHP Array Functions >PHP sort() Function
PHP sort() function is used to sort the given input array in ascending order. It sorts the actual input array hence the changes are reflected in the original array itself. It accepts two parameters $array and $sorttype. It returns the result as a boolean value TRUE on success and False in failure.
Syntax:
sort($array, $sorttype);
Parameter | Description |
---|---|
array | This is a required parameter. It defines the array to sort. |
sorttype | This is an optional parameter. It defines how to compare the array elements. |
Here is an example of sort() function in PHP:
<html> <body> <?php $arr=array("A","B","C","D","E","F","G","H"); sort($arr); $length=count($arr); for($x=0;$x<$length;$x++) { echo $arr[$x]; echo "<br>"; } ?> </body> </html>
Example 2:
<html> <body> <?php $arr=array(54,547,23,93,10,32,61,74); echo "Sorted as Number: <br>"; sort($arr,1); $length=count($arr); for($x=0;$x<$length;$x++) { echo $arr[$x]; echo "<br>"; } echo "<br>Sorted as String: <br>"; sort($arr,2); $length=count($arr); for($x=0;$x<$length;$x++) { echo $arr[$x]; echo "<br>"; } ?> </body> </html>