In this article, you will learn how to sort an indexed array in ascending order. The sort() function arranges the elements of an indexed array in ascending order.
What is the syntax of the sort() function in PHP?
sort(array, sorttype)
Parameter | Details |
---|---|
array | The array to sort in ascending order – Required |
sorttype | Optional. Specifies how to compare the array elements/items. SORT_STRING (default) – Compare elements as strings SORT_REGULAR – Compares normally without changing the type SORT_NUMERIC – Compare as numeric values SORT_LOCALE_STRING – Compare as a string according to the current locale |
Examples of sort() function
Example 1. Sort the elements of the array in ascending order alphabetically.
<?php
$arr=array("Suzuki","Corolla","Honda");
sort($arr);
?>
Example 2. Sort the elements of the array in ascending order numerically.
<?php
$n=array(5,7,4,6,23,32);
sort($n);
?>