PHP Array Sorting: This PHP tutorial helps you to sort elements of an array by several functions like sort(), asort(), ksort(), rsort() etc. With the help of several examples you will come to know more about sorting in PHP.
PHP Array Sorting: This PHP tutorial helps you to sort elements of an array by several functions like sort(), asort(), ksort(), rsort() etc. With the help of several examples you will come to know more about sorting in PHP.Array 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 )
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");
echo "<b>Before sorting the values are:</b>";
print_r($car);
asort($car);
echo"<br/>";
echo "<b>After sorting values become:</b>";
print_r($car);
?>
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 )
Another function ksort() sorts an array by key. It is useful only in associative arrays. ksort() function maintains the key to data correlation.
General Format | bool ksort(array $array [,int $sort_flags=SORT_REGULAR] ) | |
Return Value | True/False | |
Parameter | array: the input array
|
sort_flags:
|
Example 3:
<?php
$car=array("a"=>"manza","c"=>"safari","b"=>"nano","e"=>"vista","d"=>"jaguar","f"=>"land rover");
echo "<b>Before sorting the values are:</b>";
print_r($car);
ksort($car);
echo"<br/>";
echo "<b>After sorting values become:</b>";
print_r($car);
?>
Output:
Before sorting the values are:Array ( [a] => manza [c] => safari [b] => nano [e] => vista [d] => jaguar [f] => land rover )
After sorting values become:Array ( [a] => manza [b] => nano [c] => safari [d] => jaguar [e] => vista [f] => land rover )
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:
|
Example 4:
<?php
$car=array("manza","safari","nano","vista","jaguar","land rover");
echo "<b>Before sorting the values are:</b>";
print_r($car);
rsort($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] => vista [1] => safari [2] => nano [3] => manza [4] => land rover [5] => jaguar )