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 |
<?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>";
}
?>
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
<?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
<?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
<?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
<?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<?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 topAuthor
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.