PHP Program to Sort an Array in Ascending Order
Last Updated :
11 Jul, 2024
Sorting array elements is a common operation in programming. PHP provides several methods to achieve this operation. In this article, we will explore various approaches to sort array elements of an array in ascending order.
Using sort()
Function
The sort()
function is a built-in PHP function that sorts an array in ascending order.
PHP
<?php
$arr = [5, 2, 8, 3, 1];
// Sort array in ascending order
sort($arr);
// Display the sorted array
print_r($arr);
?>
OutputArray
(
[0] => 1
[1] => 2
[2] => 3
[3] => 5
[4] => 8
)
Using asort() Function
The asort() function is similar to sort(), but it maintains the association between keys and values.
PHP
<?php
// Associative array
$arr = ['b' => 5, 'a' => 2, 'd' => 8];
// Sort array by values in
// ascending order
asort($arr);
// Display the sorted array
print_r($arr);
?>
OutputArray
(
[a] => 2
[b] => 5
[d] => 8
)
Using array_multisort() Function
The array_multisort() function is used to sort multiple arrays or a multi-dimensional array.
PHP
<?php
$names = ['John', 'Anna', 'Bob'];
$ages = [30, 25, 35];
// Sorting arrays based on the
// values in the first array
array_multisort($names, $ages);
// Displaying the sorted arrays
print_r($names);
print_r($ages);
?>
OutputArray
(
[0] => Anna
[1] => Bob
[2] => John
)
Array
(
[0] => 25
[1] => 35
[2] => 30
)
Using uasort() Function with Custom Comparison Function
The uasort() function enables sorting an associative array using a user-defined comparison function.
PHP
<?php
// Associative array
$arr = ['b' => 5, 'a' => 2, 'd' => 8];
// Sort array using a custom
// comparison function
uasort($arr, function ($a, $b) {
return $a - $b;
});
// Display sorted array
print_r($arr);
?>
OutputArray
(
[a] => 2
[b] => 5
[d] => 8
)
Using Custom Function
This PHP program sorts an array in ascending order using a custom Bubble Sort function. It compares and swaps adjacent elements iteratively until the array is sorted, demonstrating an alternative to built-in sorting functions.
Example:
PHP
<?php
function bubbleSort(array $array) {
$n = count($array);
for ($i = 0; $i < $n; $i++) {
for ($j = 0; $j < $n - $i - 1; $j++) {
if ($array[$j] > $array[$j + 1]) {
// Swap the elements
$temp = $array[$j];
$array[$j] = $array[$j + 1];
$array[$j + 1] = $temp;
}
}
}
return $array;
}
// Define an array
$array = [5, 2, 9, 1, 5, 6];
// Sort the array using Bubble Sort
$sortedArray = bubbleSort($array);
// Print the sorted array
print_r($sortedArray);
?>
OutputArray
(
[0] => 1
[1] => 2
[2] => 5
[3] => 5
[4] => 6
[5] => 9
)
Using ksort() Function
The ksort() function sorts an associative array by keys in ascending order, preserving key-value associations.
Example:
PHP
<?php
// Example associative array
$array = ['c' => 3, 'a' => 1, 'b' => 2];
// Sort array by keys using ksort()
ksort($array);
print_r($array); // Output: ['a' => 1, 'b' => 2, 'c' => 3]
?>
OutputArray
(
[a] => 1
[b] => 2
[c] => 3
)
Similar Reads
How to sort an array in descending order in Ruby? In this article, we will discuss how to sort an array in descending order in ruby. We can sort an array in descending order through different methods ranging from using sort the method with a block to using the reverse method after sorting in ascending order Table of Content Sort an array in descend
3 min read
How to Check an Array is Sorted or Not in PHP? Given an array, the task is to check whether the given array is sorted or not. Arrays are often used to store and manipulate data. One common task is to check if an array is sorted in ascending or descending order. This article will explore different approaches to determine whether an array is sorte
3 min read
Sort an array of dates in PHP We are given an array of multiple dates in (Y-m-d) format. We have to write a program in PHP to sort all the dates in the array in decreasing order. Examples : Input : array("2018-06-04", "2014-06-08", "2018-06-05") Output : 2018-06-05 2018-06-04 2014-06-08 Input : array("2016-09-12", "2009-09-08",
2 min read
How to get elements in reverse order of an array in PHP ? An array is a collection of elements stored together. Every element in an array belong to a similar data type. The elements in the array are recognized by their index values. The elements can be subjected to a variety of operations, including reversal. There are various ways to reverse the elements
4 min read
PHP program to find the maximum and the minimum in array Finding the maximum and minimum in an array involves determining the largest and smallest values within a given set of numbers. This task is crucial for analyzing data, identifying trends, or filtering outliers. Various methods, from simple traversal to optimized algorithms, can achieve this.Example
7 min read