To sort an array in ascending order in PHP, you can use the built-in function sort()
.
Here is an example:
$array = array(3, 1, 5, 2, 4);
sort($array);
// Output: 1, 2, 3, 4, 5
print_r($array);
In this example, the sort()
function is used to sort the $array
in ascending order. The elements are then printed using the print_r()
function.
Another option is to use the asort()
function if you want to preserve the original keys of the array:
$array = array(3, 1, 5, 2, 4);
asort($array);
// Output: 1, 2, 3, 4, 5
print_r($array);
This code will sort the array while preserving the keys.