SQL PHP HTML ASP JavaScript articles and free scripts to download
PHP Array

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", "Pinaple","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>";
}
?>



Loking for the output? Here it is

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

-----After sort---------
0 -> Apple
1 -> Banana
2 -> Mango
3 -> Orange
4 -> Pinaple
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 ("Pinaple", "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 -> Pinaple
1 -> Orange
2 -> Banana
3 -> Mango
4 -> Apple
5 -> Strawberry

-----After rsort---------
0 -> Strawberry
1 -> Pinaple
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 ("Pinaple", "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 -> Pinaple
1 -> Orange
2 -> Banana
3 -> Mango
4 -> Apple
5 -> Strawberry

-----After asort---------
4 -> Apple
2 -> Banana
3 -> Mango
1 -> Orange
0 -> Pinaple
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 ("Pinaple", "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 -> Pinaple
1 -> Orange
2 -> Banana
3 -> Mango
4 -> Apple
5 -> Strawberry

-----After arsort---------
5 -> Strawberry
0 -> Pinaple
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" => "Pinaple", "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 -> Pinaple
d -> Orange
a -> Banana
c -> Mango
f -> Apple
e -> Strawberry

-----After ksort---------
a -> Banana
b -> Pinaple
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" => "Pinaple", "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 -> Pinaple
d -> Orange
a -> Banana
c -> Mango
f -> Apple
e -> Strawberry

-----After krsort---------
f -> Apple
e -> Strawberry
d -> Orange
c -> Mango
b -> Pinaple
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

Be the first to post comment on this article :


Google+
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy



HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2015 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer