we are going to write the code to sort an array in ascending order without using any kind of function. We will play with core programming concepts to do this task. I’m writing the scenario that we are going to solve.
Unsorted Array
Array
(
[0] => 3
[1] => 5
[2] => 8
[3] => 21
[4] => 1
[5] => 8
[6] => 5
)
Sorted Array Ascending Order
Array
(
[0] => 1
[1] => 3
[2] => 5
[3] => 5
[4] => 8
[5] => 8
[6] => 21
)
Code Example:
<center><?php ini_set('display_errors', 0); error_reporting(E_ERROR | E_WARNING | E_PARSE); ?> <?php $new= array(3,5,8,21,1,8,5); echo "<pre><h3>"; echo print_r($new); echo "<br>";echo "<br>";echo "<br>";echo "<br>";echo "<br>"; $average=0; $result=0; foreach($new as $key=> $value) { $result+=$value; } for($length=0;isset($new[$length]);$length++); for($i=0;$i<$length;$i++) { for($j=0;$j<$length;$j++) { if($new[$i]<$new[$j]) { $temp=$new[$i]; $new[$i]= $new[$j]; $new[$j]=$temp; } } } for($i=0;$i<7;$i++) { //echo $new[$i]; } echo print_r($new); ?></center>