Sorting Arrays in PHP
In this tutorial, we will explain to you about the sorting arrays in PHP. We will cover all the possible method provided by PHP with example.
Sorting Arrays in PHP, sort method, rsort method, asort method, arsort method, ksort method, krsort method, sort function, rsort function, asort function, arsort function, ksort function, krsort function, Array Sort in PHP, How to Sort Arrays in PHP, php array sort methods, PHP – sort array by array key, How to sort an array of associative arrays by value, php sort by key desc.
Checkout more articles on PHP/JavaScript
List of array sort methods
Method 1: sort()
sort()
function used to sort an array in ascending order. In the example below, we create basic php array that holds the name of flowers and sorts them in alphabetical order using this method.
1 2 3 4 5 6 7 | <?php $flowers = array("Sunflower", "Marigold", "Rose", "Lotus"); sort($flowers); print_r($flowers); ?> // Output: Array ( [0] => Lotus [1] => Marigold [2] => Rose [3] => Sunflower ) |
Here, another example for array with numbers and sorts in numerical order.
1 2 3 4 5 6 7 | <?php $numbers = array(17,28,3,92,4); sort($numbers); print_r($numbers); ?> // Output: Array ( [0] => 3 [1] => 4 [2] => 17 [3] => 28 [4] => 92 ) |
Method 2: rsort()
rsort()
function used to sort an array in descending order. Let’s see with an example.
1 2 3 4 5 6 7 | <?php $flowers = array("Sunflower", "Marigold", "Rose", "Lotus"); rsort($flowers); print_r($flowers); ?> // Output: Array ( [0] => Sunflower [1] => Rose [2] => Marigold [3] => Lotus ) |
For array with numbers.
1 2 3 4 5 6 7 | <?php $numbers = array(17,28,3,92,4); rsort($numbers); print_r($numbers); ?> // Output: Array ( [0] => 92 [1] => 28 [2] => 17 [3] => 4 [4] => 3 ) |
Method 3: asort()
asort()
function used to sort associative array by their value in ascending order. Let’s take one example of a student’s mark.
1 2 3 4 5 6 7 | <?php $marks= array("Araez"=>"75", "Henry"=>"93", "John"=>"59","George"=>"89"); asort($marks); print_r($marks); ?> // Output: Array ( [John] => 59 [Araez] => 75 [George] => 89 [Henry] => 93 ) |
Method 4: arsort()
arsort()
function used to sorts associative array by their value in descending order. Let’s take the same example.
1 2 3 4 5 6 7 | <?php $marks= array("Araez"=>"75", "Henry"=>"93", "John"=>"59","George"=>"89"); arsort($marks); print_r($marks); ?> // Output: Array ( [Henry] => 93 [George] => 89 [Araez] => 75 [John] => 59 ) |
Method 5: ksort()
ksort()
function used to sorts associative array by their key in ascending order. Let’s take the same example of student’s mark and it sorts the array by student’s name not by mark in ascending order.
1 2 3 4 5 6 7 | <?php $marks= array("Araez"=>"75", "Henry"=>"93", "John"=>"59","George"=>"89"); ksort($marks); print_r($marks); ?> // Output: Array ( [Araez] => 75 [George] => 89 [Henry] => 93 [John] => 59 ) |
Method 6: krsort()
krsort()
function used to sort associative array by their key in descending order. See below example, it sorts the array by student’s name in descending order.
1 2 3 4 5 6 7 | <?php $marks= array("Araez"=>"75", "Henry"=>"93", "John"=>"59","George"=>"89"); krsort($marks); print_r($marks); ?> // Output: Array ( [John] => 59 [Henry] => 93 [George] => 89 [Araez] => 75 ) |
That’s it for today.
Thank you for reading. Happy Coding!