Google News
logo
PHP Program to sort() - Sort array in ascending numerical order
In the following example of PHP program that demonstrates how to use the `sort()` function to sort an array of numbers in ascending numerical order :
Program :
<?php

// Create an array of numbers
$numbers = array(3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5);

// Sort the array in ascending numerical order
sort($numbers);

// Loop through the sorted array and print the values
foreach($numbers as $number) {
    echo $number . "<br>";
}

?>
Output :
1
1
2
3
3
4
5
5
5
6
9
In this example, we have created an array of numbers `$numbers`. We use the `sort()` function to sort the array in ascending numerical order.

The sorted array is then looped through using a `foreach` loop and the values of each element are printed to the screen.

The `sort()` function works by comparing the values of the elements in the array and swapping them if necessary to put them in ascending order. By default, it uses a comparison based on the values of the elements themselves.

If you want to sort the array using a custom comparison function, you can use the `usort()` function instead.