|
|
Array Sort in PHP |
We can sort array elements in different ways. We will try to learn different types of sorting here. Let us start with a simple sort command.
Here is the code for a simple sort.
The code is given here and the output is below that.
<?
$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
| Function | Applied On | Order by | Key Association |
| sort | elements | forward | altered |
| rsort | elements | reverse | altered |
| asort | elements | forward | maintained |
| arsort | elements | reverse | maintained |
| ksort | key | forward | maintained |
| krsort | key | reverse | maintained |
| |
|
|
|