Array Sort in PHP.

We can sort array elements in different ways. We will try to learn different types of sorting here.
FunctionApplied OnOrder byKey Association
sortelementsforwardaltered
rsortelementsreversealtered
asortelementsforwardmaintained
arsortelementsreversemaintained
ksortkeyforwardmaintained
krsortkeyreversemaintained
Let us start with a simple sort command.

The code is given here and the output is below that.

<?Php
$input = array ("Apple", "Orange", "Banana", "Mango", "Pineapple","Strawberry");
while (list ($key, $val) = each ($input)) {
echo "$key -> $val <br>";
}
sort($input);
echo "<br>-----After sort---------<br>";
while (list ($key, $val) = each ($input)) {
echo "$key -> $val <br>";
}
?>


Looking for the output? Here it is


0 -> Apple 
1 -> Orange
2 -> Banana
3 -> Mango
4 -> Pineapple
5 -> Strawberry

-----After sort---------
0 -> Apple
1 -> Banana
2 -> Mango
3 -> Orange
4 -> Pineapple
5 -> Strawberry
Back to top

rsort() : Reverse Sort

We can sort an array in reverse order of sort function by using rsort command. Here the index associated with each element is not retained. This is the only difference between rsort and arsort commands.

Here is the example code.

<?Php
$input = array ("Pineapple", "Orange", "Banana", "Mango", "Apple","Strawberry");
while (list ($key, $val) = each ($input)) {
echo "$key -> $val <br>";
}
rsort($input);
echo "<br>-----After rsort---------<br>";
while (list ($key, $val) = each ($input)) {
echo "$key -> $val <br>";
}
?>
Here is the output of rsort function

0 -> Pineapple 
1 -> Orange
2 -> Banana
3 -> Mango
4 -> Apple
5 -> Strawberry

-----After rsort---------
0 -> Strawberry
1 -> Pineapple
2 -> Orange
3 -> Mango
4 -> Banana
5 -> Apple
Back to top

asort() : Sort ( Index is maintained )

We can use the array sorting function asort to arrange the array elements alphabetical order. Here by using assort function we can retain or maintain the index association of each element. This can be easily explained by this example. Here is the code and the output is given below that.

<?Php
$input = array ("Pineapple", "Orange", "Banana", "Mango", "Apple","Strawberry");
while (list ($key, $val) = each ($input)) {
echo "$key -> $val <br>";
}
asort($input);
echo "<br>-----After asort---------<br>";
while (list ($key, $val) = each ($input)) {
echo "$key -> $val <br>";
}
?>
Here is the output


0 -> Pineapple 
1 -> Orange
2 -> Banana
3 -> Mango
4 -> Apple
5 -> Strawberry

-----After asort---------
4 -> Apple
2 -> Banana
3 -> Mango
1 -> Orange
0 -> Pineapple
5 -> Strawberry

arsort() : to sort in reverse ( Index is maintained )

The function arsort changes the elements and arrange them in reverse alphabetical order. Here in arsort the order is just opposite than asort.

By using arsort function we can retain the index or indices associated with elements of the original array.

Here is the example code. We have only changed the function asort to arsort.

<?Php
$input = array ("Pineapple", "Orange", "Banana", "Mango", "Apple","Strawberry");
while (list ($key, $val) = each ($input)) {
echo "$key -> $val <br>";
}
arsort($input);
echo "<br>-----After arsort---------<br>";
while (list ($key, $val) = each ($input)) {
echo "$key -> $val <br>";
}
?>
Here is the output of arsort function

0 -> Pineapple 
1 -> Orange
2 -> Banana
3 -> Mango
4 -> Apple
5 -> Strawberry

-----After arsort---------
5 -> Strawberry
0 -> Pineapple
1 -> Orange
3 -> Mango
2 -> Banana
4 -> Apple
Back to top

ksort() : Sorting array by Key

We have seen how the elements of data of the array can be sorted by using sort command. Now we will learn how to sort the key part of the array. Each key is associated with a data or element of the array. By using ksort function we can sort the array based on the key order and at the same time associated data link with the key is maintained.

Let us understand this ksort function by this example code.

<?Php
$input = array ("b" => "Pineapple", "d" =>"Orange", "a" =>"Banana", "c" =>"Mango", "f" =>"Apple","e" =>"Strawberry");
while (list ($key, $val) = each ($input)) {
echo "$key -> $val <br>";
}
ksort($input);
echo "<br>-----After ksort---------<br>";
while (list ($key, $val) = each ($input)) {
echo "$key -> $val <br>";
}
?>
Here is the output

b -> Pineapple 
d -> Orange
a -> Banana
c -> Mango
f -> Apple
e -> Strawberry

-----After ksort---------
a -> Banana
b -> Pineapple
c -> Mango
d -> Orange
e -> Strawberry
f -> Apple
Back to top

krsort() : Sorting Key by reverse order

We have seen above how the ksort function sorts the array based on the keys of the element. By using krsort we can just reverse the order of the keys. Here also we have retained the data key association after applying the krsort function.

Let us try with some examples.
<?Php
$input = array ("b" => "Pineapple", "d" =>"Orange", "a" =>"Banana", "c" =>"Mango", "f" =>"Apple","e" =>"Strawberry");
while (list ($key, $val) = each ($input)) {
echo "$key -> $val <br>";
}
krsort($input);
echo "<br>-----After krsort---------<br>";
while (list ($key, $val) = each ($input)) {
echo "$key -> $val <br>";
}
?>
The output of the above code is here

b -> Pineapple 
d -> Orange
a -> Banana
c -> Mango
f -> Apple
e -> Strawberry

-----After krsort---------
f -> Apple
e -> Strawberry
d -> Orange
c -> Mango
b -> Pineapple
a -> Banana
Back to top

All above sort methods are pre-defined way of sorting the key or elements of the array. PHP also have facility to allow users to define its own way to sort the array.

User defined way to sort the array


Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com




    Post your comments , suggestion , error , requirements etc here .




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer