PHP Sort Array | How to sort an array by Key or Value in PHP
In this article, we will learn PHP Sort Array and How to sort an array by Key or Value in PHP with example in detail.
The PHP arrays are very useful when the developers store data in variables. furthermore, by creating a specific category to group them and placing all related values into lists. therefore, sometimes arrays may contain too many values and manage these values are very complicated. furthermore, to simplify the manipulation of arrays, PHP introduces to you by PHP sort array functions.
What is sorting Array in PHP?
Sorting refers to arranging data in a specific order such as alphabetical, numerical order and increasing or decreasing order
according to some linear relationships among the data items.it also Sorting greatly improves the efficiency of searching.
Some of the functions will solely be used for associative arrays.
It’s possible to PHP Sort array by key or by value, in numerical, alphabetical, ascending and descending orders.
PHP Sorting Functions For Arrays
therefore, We will go through the given below following PHP sort array functions:
sort() – sorts arrays in ascending order
rsort() – sorts arrays in descending order
asort() – sorts associative arrays in ascending order, according to the value
ksort() – sorts associative arrays in ascending order, according to the key
arsort() – sorts associative arrays in descending order, according to the value
krsort() – sorts associative arrays in descending order, according to the key
sort()- Sort Array in Ascending Order
There is the following function sorts the elements of a numerical array in the ascending numerical order:
|
1 2 3 4 5 |
<?php $numbers = [21, 16, 71, 14, 7, 25]; sort($numbers); print_r($numbers); ?> |
|
1 |
Array ( [0] => 7 [1] => 14 [2] => 16 [3] => 21 [4] => 25 [5] => 71 ) |
Let’s see another example with a PHP array that holds the names of different Fruits names. furthermore, the code reveals how this function sorts the array in the alphabetical order:
|
1 2 3 4 5 |
<?php $fruits = ['Graps', 'Mango', 'Apple']; sort($fruits); print_r($fruits); ?> |
|
1 |
Array ( [0] => Apple [1] => Graps [2] => Mango ) |
rsort()- Sort Array in Descending Order
There is the following function which sorts the elements of a numerical array in descending numerical order.
Let’s see the example, here also use it in the same script we saw in the example with the fruits name. therefore, The change of function will produce a different result:
|
1 2 3 4 5 |
<?php $fruits = ['Graps', 'Mango', 'Apple']; rsort($fruits); print_r($fruits); ?> |
|
1 |
Array ( [0] => Mango [1] => Graps [2] => Apple ) |
Let’s do another example with the numbers. I hope you will notice the script produces an opposite result than sort() did in the previous example:
|
1 2 3 4 5 |
<?php $numbers = [21, 16, 71, 14, 7, 25]; rsort($numbers); print_r($numbers); ?> |
|
1 |
Array ( [0] => 71 [1] => 25 [2] => 21 [3] => 16 [4] => 14 [5] => 7 ) |
asort()- Sort Array in Ascending Order, According to Value
There is the following function sorts an associative array in ascending order, as per according to the value.
we will use simple examples where the values refer to girls’ age. therefore, here the values are numerical, then the PHP sort array will be sorted in that order.
|
1 2 3 4 5 6 7 8 9 |
<?php $age = array("Giselle"=>"25", "Amara"=>"15", "Josephine"=>"28", "Penelope"=>"18" ); asort($age); foreach($age as $x => $x_value) { print_r ("Key=" . $x . ", Value=" . $x_value); print_r ("<br>"); } ?> |
|
1 2 3 4 |
Key=Amara, Value=15 Key=Penelope, Value=18 Key=Giselle, Value=25 Key=Josephine, Value=28 |
ksort()- Sort Array in Ascending Order, According to Key
The is the following function that sorts an associative array in ascending order, as per according to the key:
|
1 2 3 4 5 6 7 8 9 |
<?php $age = array("Giselle"=>"25", "Amara"=>"15", "Josephine"=>"28", "Penelope"=>"18" ); ksort($age); foreach($age as $x => $x_value) { print_r ("Key=" . $x . ", Value=" . $x_value); print_r ("<br>"); } ?> |
|
1 2 3 4 |
Key=Amara, Value=15 Key=Giselle, Value=25 Key=Josephine, Value=28 Key=Penelope, Value=18 |
arsort()- Sort Array in Descending Order, According to Value
Therefore, The following function is used to sorts an associative array in descending order, according to the value.
|
1 2 3 4 5 6 7 8 9 |
<?php $age = array("Giselle"=>"25", "Amara"=>"15", "Josephine"=>"28", "Penelope"=>"18" ); arsort($age); foreach($age as $x => $x_value) { print_r ("Key=" . $x . ", Value=" . $x_value); print_r ("<br>"); } ?> |
|
1 2 3 4 |
Key=Josephine, Value=28 Key=Giselle, Value=25 Key=Penelope, Value=18 Key=Amara, Value=15 |
krsort()- Sort Array in Descending Order, According to Key
There is the following function sorts an associative array in descending order, as per according to the key.
|
1 2 3 4 5 6 7 8 9 |
<?php $age = array("Giselle"=>"25", "Amara"=>"15", "Josephine"=>"28", "Penelope"=>"18" ); krsort($age); foreach($age as $x => $x_value) { print_r ("Key=" . $x . ", Value=" . $x_value); print_r ("<br>"); } ?> |
|
1 2 3 4 |
Key=Penelope, Value=18 Key=Josephine, Value=28 Key=Giselle, Value=25 Key=Amara, Value=15 |
The PHP array ksort() and krsort() function make PHP sort associative arrays, by their key.
Overview: PHP Sort Array
PHP has several functions which are deal with sorting arrays, and this functions to help sort it all out.
There are the following main differences:
- Some PHP sort Array based on the array keys, whereas others by the values, like as:
$array['key'] = 'value'; - Whether or not the correlation between the keys and values is maintained after the sort, therefore, means the keys are reset numerically (0,1,2 …)
- The order of the sort: alphabetical, low to high
(ascending order), high to low(descending order), natural, numerical, random, or user-defined - All of those functions act directly on the array variable itself, such as opposed to returning a new sorted array
- If any of these kind sort functions evaluate two (2) members as equal then the order is undefined (means that the sorting is not stable).
Conclusion: PHP Sort Array
With this we come to an end of this article, therefore, I hope you have learned about the all the PHP array sort functions. You can easily be sorting PHP arrays using PHP inbuilt functions. It’s possible to PHP sort array by value or by key, in alphabetical, numerical, descending and ascending orders. as you saw that Some functions only can be applied for associative arrays.
I hope you will be enjoyed this article, if you have any query, contact me on mail info@tutorialscan.com. I will resolve the problem.