Certainly! Here’s an example of how you can reorder an array without using any built-in functions in PHP:

function bubbleSort($array) { $length = count($array); for ($i = 0; $i < $length - 1; $i++) { for ($j = 0; $j < $length - $i - 1; $j++) { if ($array[$j] > $array[$j + 1]) { // Swap array elements $temp = $array[$j]; $array[$j] = $array[$j + 1]; $array[$j + 1] = $temp; } } } return $array; } // Sample array $array = [7, 2, 4, 1, 5]; // Function to find the length of an array function getArrayLength($arr) { $count = 0; foreach ($arr as $element) { $count++; } return $count; } // Function to print an array function printArray($arr) { foreach ($arr as $element) { echo $element . " "; } } // Reorder array using bubble sort $arrayLength = getArrayLength($array); for ($i = 0; $i < $arrayLength - 1; $i++) { for ($j = 0; $j < $arrayLength - $i - 1; $j++) { if ($array[$j] > $array[$j + 1]) { // Swap array elements $temp = $array[$j]; $array[$j] = $array[$j + 1]; $array[$j + 1] = $temp; } } } // Print the reordered array printArray($array);

In this program, we have an array [7, 2, 4, 1, 5] that we want to reorder without using any built-in functions.

First, we define a function bubbleSort() that implements the bubble sort algorithm. It takes an array as input, performs consecutive passes over the array, and swaps adjacent elements if they are in the wrong order. The process continues until the array is completely sorted.

Then, we define a couple of additional functions: getArrayLength() calculates the length of an array by iterating over its elements, and printArray() simply prints the elements of an array.

Next, we manually apply the bubble sort algorithm to reorder the array. We use two nested for loops to iterate through the array and swap elements whenever necessary.

Finally, we call the printArray() function to print the reordered array. In this case, the output will be 1 2 4 5 7.

Leave a Reply

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