All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Sort Array In PHP Without Using Function

Last Updated : Mar 11, 2024

Sort Array In PHP Without Using Function

In this tutorial we will show you the solution of sort array in php without using function, today we are going to understand how to sort array in php without using function. Mainly there are many inbuilt function in php like sort (), rsort(), ksort(), krsort(), assort() arsort(), push() and pop(), etc.

Hence, there are many functions with help of which we can sort array in php.

But today we want to sort array without using inbuilt functions. Now, let us see how to sort array in php without using inbuilt functions.

Step By Step Guide On Sort Array In Php Without Using Function :-

Here, first of all there are many ways with help of which we are able to sort arrays.

Today we are going to use for loop to done to sort an array.

For example, let us assume that there is an array containing integer values like 2, 4, 88, 5, 7, 1, 6, 9, 10, 3, and 15. Now, we are going to sort this array using below code.

<!DOCTYPE html>
      <html>
        <head>
          <title> how to sort array in php without using built in functions </title>
   </head>
 <body>
 <?php
 $array=array('2','4','88','5','7','1','6','9','10','3',’15’);
echo "Given array is: ";
echo "<br />";
print_r($array);
for($j = 0; $j < count($array); $j ++) {
    for($i = 0; $i < count($array)-1; $i ++){
        if($array[$i] > $array[$i+1]) {
            $temp = $array[$i+1];
            $array[$i+1]=$array[$i];
            $array[$i]=$temp;
        }
    }
}
echo " After Sorting of Array : ";
echo "<br />";
print_r($array);
 ?>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. Here, as we see that inside body we create an array that is a multidimensional indexed array. We hope that you know the difference between indexed array, associative array and multidimensional array.
  6. Here, as we see that we create an array and store that values inside $array variable. Now, here to sort an array we use for loop here. And for loop is used two times here, will discuss it below.
  7. After as you can see we create two loops that are for loop these all are used to sort array. let us understand these.
  8. Here, we found that we pick up a value and compare it with next value.
  9. If next found is found lower that current value then it stores to temporary variable and current values get changed to new value.
  10. This process repeated again and again until loop finishes.
  11. At last, we print the sorted array to show that array gets sorted.
  12. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to sort array in php without using inbuilt functions.

I hope this tutorial on sort array in php without using function helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪