PHP Sorting Arrays
Sorting an array is the most useful thing you
can do with an array. We will learn how to sort an array using key as well as
value.
Sort By Value:
PHP provides many functions to sort an array. We can sort an array by value, key or randomly. But in most of the situations we need to sort an array by value. There are so many functions are available in PHP, some of these are discussed below:
i) sort()
ii) asort()
iii) rsort()
iv) arsort()
v) natsort()
vi) natcasesort()
i) sort(): To sort an array we need to use sort() function. Small description of sort is given below:
General Format | bool sort(array $array [,int $sort_flags=SORT_REGULAR] ) | |
Return Value | True/False | |
Parameters | array: the input array | sort_flags:
|
Example 1:
<?php
$car=array("manza","safari","nano","vista","jaguar","land rover");
echo "<b>Before sorting the values are:</b>";
print_r($car);
sort($car);
echo"<br/>";
echo "<b>After sorting values become:</b>";
print_r($car);
?>
Output:
Before sorting the values are: Array ( [0] => manza [1] =>
safari [2] => nano [3] => vista [4] => jaguar [5] => land rover )
After sorting values become: Array ( [0] => jaguar [1] => land
rover [2] => manza [3] => nano [4] => safari [5] => vista )
ii) asort(): If we examine the above code and the result we can see that after being sorted the elements of the array are arranged in alphabetical order. Another method to sort the array is asort(). This function sorts an array and also maintain the index position. Small description of asort() is:
General Format | bool asort(array $array [,int $sort_flags=SORT_REGULAR] ) | |
Return Value | True/False | |
Parameter | array: the input array
|
sort_flags:
|
Example 2:
<?php
$car=array("m"=>"manza","s"=>"safari","n"=>"nano","v"=>"vista","j"=>"jaguar","l"=>"land rover"); 0
echo "<b>Before sorting the values are:</b>";
print_r($car);
asort($car); 1
echo"<br/>";
echo "<b>After sorting values become:</b>";
print_r($car); 2
?>
Output:
Before sorting the values are:Array ( [m] => manza [s] => safari
[n] => nano [v] => vista [j] => jaguar [l] => land rover )
After sorting values become:Array ( [j] => jaguar [l] => land rover
[m] => manza [n] => nano [s] => safari [v] => vista )
3
iii) rsort(): There are several functions are available in PHP, rsort() is one of them, it sorts an array in reverse order, rsort() is just opposite sort() function:
General Format | bool rsort(array $array [,int $sort_flags=SORT_REGULAR] ) | |
Return Value | True/False | |
Parameter | array: the input array
|
sort_flags:
|
4
Example 3:
<?php
$car=array("manza","safari","nano","vista","jaguar","land rover"); 5
echo "<b>Before sorting the values are:</b>";
print_r($car);
rsort($car); 6
echo"<br/>";
echo "<b>After sorting values become:</b>";
print_r($car); 7
?>
Output:
Before sorting the values are:Array ( [0] => manza [1] => safari
[2] => nano [3] => vista [4] => jaguar [5] => land rover )
After sorting values become:Array ( [0] => vista [1] => safari [2]
=> nano [3] => manza [4] => land rover [5] => jaguar )
8
iv) arsort(): If we consider the name of the function then it will be clear that this function will sort an array in reverse order and maintain index association like asort() + rsort() + sort()=arsort(). Small description of the function is as follows:
General Format | bool arsort(array $array [,int $sort_flags=SORT_REGULAR] ) | |
Return Value | True/False | |
Parameter | array: the input array
9 |
sort_flags:
|
Example 4:
<?php
$car=array("m"=>"manza","s"=>"safari","n"=>"nano","v"=>"vista","j"=>"jaguar"); 0
echo "<b>Before sorting the values are:</b>";
print_r($car);
arsort($car); 1
echo"<br/>";
echo "<b>After sorting values become:</b>";
print_r($car); 2
?>
Output:
Before sorting the values are:Array ( [m] => manza [s] => safari
[n] => nano [v] => vista [j] => jaguar )
After sorting values become:Array ( [v] => vista [s] => safari [n]
=> nano [m] => manza [j] => jaguar )
3
v) natsort(): This function of PHP sorts an array using "natural order" algorithm, meaning of "natural order" algorithm is it sorts alphanumeric strings in such a way that human sorts the key/value associations. Description is as follows:
General Format | bool natsort(array $array) |
Return Value | True/False |
Parameter | array: the input array
4 |
Example 5:
<?php
$array1=$array2=array("Emp01","Emp10","Emp23","Emp04"); 5
echo"<b> Before Simple sorting the values are:</b><br/>";
print_r($array1);
sort($array1); 6
echo"<br/><b> After Simple sorting the values are:</b><br/>";
print_r($array1);
natsort($array2); 7
echo"<br/><b> After natural sorting the values are:</b><br/>";
print_r($array2);
?> 8
Output:
Before Simple sorting the values are:
Array ( [0] => Emp01 [1] => Emp10 [2] => Emp23 [3] => Emp04 )
After Simple sorting the values are:
Array ( [0] => Emp01 [1] => Emp04 [2] => Emp10 [3] => Emp23 )
After natural sorting the values are:
Array ( [0] => Emp01 [3] => Emp04 [1] => Emp10 [2] => Emp23 )
9
vi) natcasesort(): Sort an array using a case insensitive "natural algorithm" manner. To know more about natural algorithm please read natsort() which is given above. Description of natcasesort() is given below:
General Format | bool natcasesort(array $array) |
Return Value | True/False |
Parameter | array: the input array
|
Example 6: 0
<?php
$array1=$array2=$array3=array("emp01","Emp10","emp23","Emp04");
echo"<b> Before Simple sorting the values are:</b><br/>"; 1
print_r($array1);
echo"<br/><b> After Simple sorting the values are:</b><br/>";
sort($array1); 2
print_r($array1);
echo"<br/><b> After natural sorting the values are:</b><br/>";
natsort($array2); 3
print_r($array2);
echo"<br/><b> After natural case insensitive sorting the values are:</b><br/>";
natcasesort($array3); 4
print_r($array3);
?>
Output: 5
Before Simple sorting the values are:
Array ( [0] => emp01 [1] => Emp10 [2] => emp23 [3] => Emp04 )
After Simple sorting the values are:
Array ( [0] => Emp04 [1] => Emp10 [2] => emp01 [3] => emp23 )
After natural sorting the values are:
Array ( [3] => Emp04 [1] => Emp10 [0] => emp01 [2] => emp23 )
After natural case insensitive sorting the values are:
Array ( [0] => emp01 [3] => Emp04 [1] => Emp10 [2] => emp23 )
Sort Array by Key 6
In PHP there are are so many built in functions are available which make this language easier. In general we sort the values of an array, but sometimes we need to sort the keys instead of values, PHP provided us three functions to sort any array by key. These are:
i) ksort()
ii) krsort() 7
Now we will discuss these functions one by one in detail:
i) ksort(): This function sorts an array by key maintaining the correlation with the value, this function is useful for associative array.
General Format | bool ksort(array $array [,int $sort_flags=SORT_REGULAR] ) | |
Return Value | True/False | |
Parameters | array: the input array | sort_flags:
|
8
Example 1:
<?php
$array=array(30=>"twitter",11=>"orkut",7=>"facebook"); 9
echo"<b>Before sorting the keys of the array is:</b><br/>";
print_r($array);
ksort($array); 0
echo"<br/><b>Before sorting the keys of the array is:</b><br/>";
print_r($array);
?> 1
Output:
Before sorting the keys of the array is:
Array ( [30] => twitter [11] => orkut [7] => facebook )
After sorting the keys of the array is:
Array ( [7] => facebook [11] => orkut [30] => twitter )
From the above example we can see that the sorting is done on the key instead of value. 2
ii) krsort(): This function sorts an array by key in reverse order maintaining the correlation with the value, this function is useful for associative array.
General Format | bool krsort(array $array [,int $sort_flags=SORT_REGULAR] ) | |
Return Value | True/False | |
Parameters | array: the input array | sort_flags:
|
Example 2: 3
<?php
$array=array(30=>"twitter",11=>"orkut",7=>"facebook");
echo"<b>Before sorting the keys of the array is:</b><br/>"; 4
print_r($array);
krsort($array);
echo"<br/><b>After sorting the keys in reverse order:</b><br/>"; 5
print_r($array);
?>
Output: 6
Before sorting the keys of the array is:
Array ( [30] => twitter [11] => orkut [7] => facebook )
After sorting the keys in reverse order:
Array ( [30] => twitter [11] => orkut [7] => facebook )