Sort Array In PHP Without Using Function

In this post we will learn how to sort array value in PHP without using in built function, PHP provide many array sort function like: sort(), arsort(), ksort(), krsort() etc, but we will learn without these functions.

Now let us start how to sort array without using PHP inbuilt function

Step by step guid for sort array in PHP.

First, we will take an array with multiple elements and then using loop we will do the sorting.

  1. Array Sort in ascending order.
<?php 
$array = [10,103,2,94,11,2,58,6,4,3,10,100];
//Given Array
print_r($array);
for($j = 0; $j < count($array); $j ++) {
    for($i = 0; $i < count($array)-1; $i ++){
    if($array[$i]>$array[$i+1]){
         $max = $array[$i+1];
         $array[$i+1]=$array[$i];
         $array[$i] = $max;
    }
   
}}
//Sort Array
print_r($array);

?>

2) Array sorting de-sending order

<?php 
$array = [10,103,2,94,11,2,58,6,4,3,10,100];
//Given Array
print_r($array);
for($j = 0; $j < count($array); $j ++) {
    for($i = 0; $i < count($array)-1; $i ++){
    if($array[$i]<$array[$i+1]){
         $max = $array[$i+1];
         $array[$i+1]=$array[$i];
         $array[$i] = $max;
    }
   
}}
//Sort Array
print_r($array);

?>

Conclusion :-

At last, in conclusion we can say that we are able to understand using this post how to sort array in php without using php inbuilt function.

I can hope that this post for sort an array without using php functions helps you.

Keep Learning 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *