Sort Function in PHP

Arrays Sort Functions

In PHP, The sort() function sorts an array in ascending order.

Syntax: 

sort(arr, flag)
Here -
arr means which array you want to sort.
flag means which sort process you are going to apply on array.

The array sort() function is responsible to sort the array. array sort function returns TRUE on success and otherwise it returns FALSE.

In the following, I am going to explain sort function by given two examples.

Example 1: 

<?php 
$arr1 = array("jackie", "tina", "sojen","daniel" ); 
sort($arr1, SORT_STRING); 
print_r($arr1); 
?>

Output of the above example:
Array ( 
[0] => daniel 
[1] => jackie 
[2] => sojen 
[3] => tina 
)
Lets see another example with sort function.

Example 2: 

<?php 
$arr1 = array(50, 60, 40, 70, 30, 20, 11); 
sort($arr1); 
print_r($arr1); 
?>

Output of the above example:
Array ( 
[0] => 11
[1] => 20
[2] => 30
[3] => 40
[4] => 50
[5] => 60
[6] => 70
)
In this post, I am going to explain the following PHP array sort functions:
  • sort() - This sort functions sort arrays in ascending order 
  • rsort() - This sort functions sort arrays in descending order 
  • asort() - This sort functions sort associative arrays in ascending order, according to the value 
  • ksort() - This sort functions sort associative arrays in ascending order, according to the key 
  • arsort() - This sort functions sort associative arrays in descending order, according to the value 
  • krsort() - This sort functions sort associative arrays in descending order, according to the key
sort() - This Sort function Sort Array in Ascending Order:
Actually sort() function sorts an array in ascending order. In The following example sorts the elements of the $letter array in ascending alphabetical order:
<!DOCTYPE html> 
<html> 
<body> 
<?php 
$letter = array("c", "b", "a"); 
sort($letter); 
$length = count($letter); 
for($x = 0; $x < $length; $x++) { 
echo $letter[$x]; echo "<br>"; 
?> 
</body> 
</html>

Output of the above example:
Array ( 
[0] => a
[1] => b
[2] => c
)
The following example sorts the elements of the $numbers array in ascending numerical order:
<!DOCTYPE html> 
<html> 
<body> 
<?php 
$numbers = array(50, 11, 5, 22, 41); 
sort($numbers); 
$length = count($numbers); 
for($a = 0; $a < $length; $a++) { 
echo $numbers[$a]; echo "<br>"; 
?> 
</body> 
</html>

Output of the above example:
Array ( 
[0] => 5
[1] => 11
[2] => 22
[3] => 41
[4] => 50
)
rsort() - This Sort function Sort Array in Descending Order:
Actually sort() function sorts an array in descending order. The following example sorts the elements of the $letter array in descending alphabetical order:
<!DOCTYPE html> 
<html> 
<body> 
<?php 
$letter = array("a", "b", "c"); 
rsort($letter); 
$length = count($letter); 
for($x = 0; $x < $length; $x++) { 
echo $letter[$x]; echo "<br>"; 
?> 
</body> 
</html>

Output of the above example:
Array ( 
[0] => c
[1] => b
[2] => a
)
The following example sorts the elements of the $numbers array in descending numerical order:
<!DOCTYPE html> 
<html> 
<body> 
<?php 
$numbers = array(50, 11, 5, 22, 41); 
rsort($numbers); 
$length = count($numbers); 
for($a = 0; $a < $length; $a++) { 
echo $numbers[$a]; echo "<br>"; 
?> 
</body> 
</html>

Output of the above example:
Array ( 
[0] => 50
[1] => 41
[2] => 22
[3] => 11
[4] => 5
)
asort() - This function sorts an array in Ascending Order According to Value not the key:
The below example sorts an associative array in ascending order, according to the value not the key:
<!DOCTYPE html> 
<html> 
<body> 
<?php 
$age = array("Ahamed"=>"56", "Ben"=>"40", "Carry"=>"51"); 
asort($age); 
foreach($age as $key => $value) { 
echo "Key=" . $key . ", Value=" . $value; echo "<br>"; 
?> 
</body> 
</html>

Result:
Key=Ben, Value=40
Key=Carry, Value=51
Key=Ahamed, Value=56
ksort() - This function sorts an array in Ascending Order According to Key not the Value:
The below example sorts an associative array in ascending order, according to the key not the value:
<!DOCTYPE html> 
<html> 
<body> 
<?php 
$age = array("carry"=>"56", "Ben"=>"40", "Ahamed"=>"51"); 
asort($age); 
foreach($age as $key => $value) { 
echo "Key=" . $key . ", Value=" . $value; echo "<br>"; 
?> 
</body> 
</html>

Result:
Key=Ahamed, Value=51 
Key=Ben, Value=40 
Key=carry, Value=56
arsort() - This function sorts an array in descending Order According to Value not the key
The below example sorts an associative array in descending order, according to the value only not the key:
<!DOCTYPE html> 
<html> 
<body> 
<?php 
$age = array("carry"=>"56", "Ben"=>"40", "Ahamed"=>"51"); 
arsort($age); 
foreach($age as $key => $value) { 
echo "Key=" . $key . ", Value=" . $value; echo "<br>"; 
?> 
</body> 
</html>

Result:
Key=carry, Value=56 
Key=Ahamed, Value=51 
Key=Ben, Value=40
krsort() - This function sorts an array in descending Order According to Key Only not the Value:
The below example sorts an associative array in descending order, according to the key only not the value:
<!DOCTYPE html> 
<html> 
<body> 
<?php 
$age = array("carry"=>"56", "Ben"=>"40", "Ahamed"=>"51"); 
krsort($age); 
foreach($age as $key => $value) { 
echo "Key=" . $key . ", Value=" . $value; echo "<br>"; 
?> 
</body> 
</html>

Result:
Key=carry, Value=56 
Key=Ben, Value=40 
Key=Ahamed, Value=51

Sorting Multi-Dimensional Arrays in PHP:

In real life, it is common to deal with multi-dimensional information. Assume A school will store the marks of all students with variety of subjects in a single table instead of creating new tables for each subject. In PHP, If you want to store the same information, Then its an suggest to do it using a multi-dimensional array instead of a separate array for each subject. 

A Similar Example, If you wan to store a tallest buildings information in a single table you can use multi-dimensional array to do this.

In real Life, we face many problem to store with multi-dimensional information. In this case we can easily solve the problem using the multi-dimensional array.

In this Post, I am going to show how to sort a multi-dimensional array using a list of tallest buildings in the world with an example in the below. Assume we need to store many information about that building as such as name of the building, the city name, Country name where the building is located, The number of floors, total height of the building in meter and the established year in which buildings was built. All of those information we need to store in a database table.

Now we need to sort the arrays particular values.
You are going to sort the values in a multi-dimensional array based on a particular field, then you can use the usort() function to sort this array. With the following example you will much more clear with practical knowledge.

usort() function to sort the following example:

<?php
$tallestBuildings = [
    ["Building" => "ABC","City" => "New York","Country" => "America","Height" => 800,"Floors" => 152,"Year" => 2010],
    ["Building" => "ABC 2","City" => "Shanghai","Country" => "China","Height" => 700,"Floors" => 125,"Year" => 2011],
    ["Building" => "ABC 3","City" => "Dhaka","Country" => "Saudi","Height" => 500,"Floors" => 101,"Year" => 2012]
];
function storey_sort($building_a, $building_b) {
    return $building_a["Floors"] - $building_b["Floors"];
}
usort($tallestBuildings, "storey_sort");
foreach($tallestBuildings as $tallBuilding) {
    list($building, $city, $country, $height, $floors) = array_values($tallBuilding);
    echo $building." is located in ".$city.", ".$country.". This building is ".$height." meters tall with ".$floors." floors.\n";
}
?>


EmoticonEmoticon