PHP Program to sort array in ascending order


March 22, 2022, Learn eTutorial
1287

Array in ascending order

In this program, we are going to sort the elements of an array in ascending order. To sort the elements in ascending order we have to compare every element of the array with each other. For example, the array is 56,8,12,9,87  then the output will be 87,56,12,9,8.

How to sort elements of the array in ascending order using PHP?

 For this program to sort the elements in an array we are taking the static values which are assigned to the array a[]. Then we have to assign the value 0 into the variable temp to store temporarily and also assign the size of the array into the variable len using the built-in function count() and after that, we have to assign the value 0 into the variable i and perform the loop until the condition 'i < len' and also increment the value of the variable i in every iteration and in the loop block we have to assign the value 1 into the variable j and perform the loop until the condition 'j < len' and also increment the value of the variable j in every iteration and in the loop block check the condition 'a[i] > a[j]' if true assign the value of a[i] into the variable temp, assign the value of a[j] into a[i] and assign the value of temp into a[j]  after the completion of the loops we can print the elements of the array in the ascending order by using the for loop.

ALGORITHM

Step 1: Initialize an array a[] with values

Step 2: Create a temporary variable temp with a value of 0

Step 3: Assing the size of an array into the variable len using the built-in function count()

Step 4: Print the element in the array a[]

Step 5: Assign the value 0 into the variable i and perform the step 6 until the condition 'i < len' becomes false and increment the value of variable i in every iteration

Step 6: Assign the value 1 into the variable j and perform the sub-steps until the condition 'j < len' becomes false and increment the value of variable j in every iteration

        (i) Check the condition 'a[i] > a[j]' if true perform the sub-steps

            (a) Assign the value of a[i] into variable temp

            (b) Assign the value of a[j] into a[i]

            (c) Assign the value of temp into a[j]

Step 5: Print the elements of the array a[] as in ascending order

PHP Source Code

                                          <?php
$a = array(58, 12, 86, 37, 1);
$temp = 0;
$len = count($a);
echo "Elements in array are:\n";
for ($i = 0; $i < $len; $i++) {
    echo "$a[$i] ";
}
for ($i = 0; $i < $len; $i++) {
    for ($j = $i + 1; $j < $len; $j++) {
        if ($a[$i] > $a[$j]) {
            $temp = $a[$i];
            $a[$i] = $a[$j];
            $a[$j] = $temp;
        }
    }
}
echo "\nElements of array sorted in ascending order: \n";
for ($i = 0; $i < $len; $i++) {
    echo "$a[$i] ";
}
?>
                                      

OUTPUT

Elements in array are:
58 12 86 37 1
Elements of array sorted in ascending order:
1 12 37 58 86