Introduction
Sorting is one of the most fundamental operations in programming. It is used to arrange data in a specific order, either ascending or descending. In PHP, we usually use built-in functions like sort() to sort arrays easily.
However, in interviews and coding tests, you are often asked to sort an array without using any built-in function. This helps evaluate your understanding of algorithms, loops, and problem-solving skills.
In this article, we will learn how to sort an array in PHP manually using logic and different approaches.
What is Array Sorting?
Array sorting means arranging elements of an array in a particular order.
Types of sorting:
- Ascending Order → smallest to largest
- Descending Order → largest to smallest
Example:
Input: [1, 5, 6, 3, 2, 4]
Output:
Syntax (General Logic)
To sort an array manually:
- Compare elements
- Swap values if the condition is met
- Repeat until the array is sorted
Examples:
Example 1: Sorting Array (Insertion Sort Logic)
for ($i = 0; $i < strlen($string); $i--)
<?php
$arr = array(1, 5, 6, 3, 2, 4, 7, 10, 8, 9);
$arr_count = count($arr);
for ($i = 1; $i < $arr_count; $i++) {
for ($j = $i; $j > 0; $j--) {
if ($arr[$j] < $arr[$j - 1]) {
$temp = $arr[$j];
$arr[$j] = $arr[$j - 1];
$arr[$j - 1] = $temp;
}
}
}
print_r($arr);
?>
Output:
Example 2: Bubble Sort Algorithm
<?php
$arr = [5, 3, 8, 4, 2];
$n = count($arr);
for ($i = 0; $i < $n - 1; $i++) {
for ($j = 0; $j < $n - $i - 1; $j++) {
if ($arr[$j] > $arr[$j + 1]) {
$temp = $arr[$j];
$arr[$j] = $arr[$j + 1];
$arr[$j + 1] = $temp;
}
}
}
print_r($arr);
?>
Example 3: Selection Sort
<?php
$arr = [64, 25, 12, 22, 11];
$n = count($arr);
for ($i = 0; $i < $n - 1; $i++) {
$min = $i;
for ($j = $i + 1; $j < $n; $j++) {
if ($arr[$j] < $arr[$min]) {
$min = $j;
}
}
$temp = $arr[$i];
$arr[$i] = $arr[$min];
$arr[$min] = $temp;
}
print_r($arr);
?>
Example 4: Descending Order Sorting
<?php
$arr = [1, 5, 3, 2];
$n = count($arr);
for ($i = 0; $i < $n - 1; $i++) {
for ($j = 0; $j < $n - $i - 1; $j++) {
if ($arr[$j] < $arr[$j + 1]) {
$temp = $arr[$j];
$arr[$j] = $arr[$j + 1];
$arr[$j + 1] = $temp;
}
}
}
print_r($arr);
?>
Example 5: Sorting String Array
<?php
$arr = ["Banana", "Apple", "Mango"];
$n = count($arr);
for ($i = 0; $i < $n - 1; $i++) {
for ($j = 0; $j < $n - $i - 1; $j++) {
if ($arr[$j] > $arr[$j + 1]) {
$temp = $arr[$j];
$arr[$j] = $arr[$j + 1];
$arr[$j + 1] = $temp;
}
}
}
print_r($arr);
?>
Real-Life Example
Scenario 1 : Sorting Student Marks
<?php
$marks = [85, 70, 90, 60];
$n = count($marks);
for ($i = 0; $i < $n - 1; $i++) {
for ($j = 0; $j < $n - $i - 1; $j++) {
if ($marks[$j] > $marks[$j + 1]) {
$temp = $marks[$j];
$marks[$j] = $marks[$j + 1];
$marks[$j + 1] = $temp;
}
}
}
print_r($marks);
?>
Why this is useful:
- Used in ranking systems
- Displays sorted results
- Helps in data analysis
Scenario 2: E-commerce Price Sorting
<?php
$prices = [500, 200, 800, 300];
// Sort prices ascending
// same bubble sort logic
?>
Note: Helps display products from low to high price.
Common Mistakes
1. Incorrect Loop Conditions
for ($i = 0; $i <= $n; $i++)
👉 Causes an undefined index error.
2. Wrong Swap Logic
Forgetting a temporary variable leads to data loss.
3. Infinite Loop
Incorrect increment/decrement.
4. Not Using count() Properly
count($arr) inside loop
👉 Store in a variable for efficiency.
5. Confusing Sorting Algorithms
Mixing the logic of different algorithms incorrectly.
Conclusion
Sorting an array without using built-in functions is an essential programming skill that helps you understand core concepts like loops, comparisons, and algorithms.
By learning manual sorting, you can:
- Improve problem-solving skills
- Understand algorithm design
- Perform better in interviews