sort() – The sort() function is used for sorting the arrays in ascending order.
<?php
$books = array(“Java”, “PHP”, “Dotnet”);
sort($books);
$book_length = count($books);
for($x = 0; $x < $book_length; $x++) {
echo $books[$x];
echo “<br>”;
}
?>
Output as :
Dotnet
Java
PHP
Here , The books are shorted in the ascending order on the basis of alphabets
<?php
$num = array(4, 6, 2, 22, 11);
sort($num);
$arrlen = count($num);
for($x = 0; $x < $arrlen; $x++) {
echo $num[$x];
echo “,”;
}
?>
Output As :
2,4,6,11,22,
Here , the numbers values from the arrays are sorted in the form af ascending order .
rsort() – The rsort() function sorts the arrays in descending order.
<?php
$books = array(“Java”, “PHP”, “Dotnet”);
rsort($books);
$book_length = count($books);
for($x = 0; $x < $book_length; $x++) {
echo $books[$x];
echo “<br>”;
}
?>
The output will be just reverse of the above one i.e sort() function as
PHP
Java
Dotnet
<?php
$num = array(4, 6, 2, 22, 11);
rsort($num);
$arrlen = count($num);
for($x = 0; $x < $arrlen; $x++) {
echo $num[$x];
echo “,”;
}
?>
The output produce just reverse to sort() function i.e
22,11,6,4,2,
asort() – The asort() sorts the associative arrays in ascending order, according to the value.
<?php
$marks = array(“php”=>”99”, “java”=>”89”, “dotnet”=>”79”);
asort($marks);
foreach($marks as $x => $mark) {
echo “Key=” . $x . “, Value=” . $mark;
echo “<br>”;
}
?>
Output As :
Key=dotnet, Value=79
Key=java, Value=89
Key=php, Value=99
<?php
$authors = array(“php”=>”me”, “java”=>”you”, “dotnet”=>”he”);
asort($authors);
foreach($authors as $x => $auth) {
echo “Key=” . $x . “, Value=” . $auth;
echo “<br>”;
}
?>
Output As :
Key=dotnet, Value=he
Key=php, Value=me
Key=java, Value=you
Here , from the above both examples you can see the the asort() the values on the basis of values in arrays in ascending order .
ksort() – The ksort() sorts the associative arrays in ascending order, according to the key.
<?php
$marks = array(“php”=>”99”, “java”=>”89”, “dotnet”=>”79”);
ksort($marks);
foreach($marks as $x => $mark) {
echo “Key=” . $x . “, Value=” . $mark;
echo “<br>”;
}
?>
Output As :
Key=dotnet, Value=79
Key=java, Value=89
Key=php, Value=99
<?php
$authors = array(“php”=>”me”, “java”=>”you”, “dotnet”=>”he”);
ksort($authors);
foreach($authors as $x => $auth) {
echo “Key=” . $x . “, Value=” . $auth;
echo “<br>”;
}
?>
Output As :
Key=dotnet, Value=he
Key=java, Value=you
Key=php, Value=me
From the above example it is clear the ksort() sorts the values on the behalf of keys where as sort() in the basis of the values in arrays in ascending orders.
arsort() – The arsort() sorts the associative arrays in descending order, according to the value.
<?php
$marks = array(“php”=>”99”, “java”=>”89”, “dotnet”=>”79”);
arsort($marks);
foreach($marks as $x => $mark) {
echo “Key=” . $x . “, Value=” . $mark;
echo “<br>”;
}
?>
Output As :
Key=php, Value=99
Key=java, Value=89
Key=dotnet, Value=79
<?php
$authors = array(“php”=>”me”, “java”=>”you”, “dotnet”=>”he”);
arsort($authors);
foreach($authors as $x => $auth) {
echo “Key=” . $x . “, Value=” . $auth;
echo “<br>”;
}
?>
Output As :
Key=java, Value=you
Key=php, Value=me
Key=dotnet, Value=he
So , From the above example you can see that the shorting is just reverse to asort() function.
i.e sorting associative arrays in descending order by value .
krsort() – The krsort() sorts the associative arrays in descending order, according to the key.
$marks = array(“php”=>”99”, “java”=>”89”, “dotnet”=>”79”);
krsort($marks);
foreach($marks as $x => $mark) {
echo “Key=” . $x . “, Value=” . $mark;
echo “<br>”;
}
?>
Output As :
Key=php, Value=99
Key=java, Value=89
Key=dotnet, Value=79
<?php
$authors = array(“php”=>”me”, “java”=>”you”, “dotnet”=>”he”);
krsort($authors);
foreach($authors as $x => $auth) {
echo “Key=” . $x . “, Value=” . $auth;
echo “<br>”;
}
?>
Output As :
Key=php, Value=me
Key=java, Value=you
Key=dotnet, Value=he
So , From the above example you can see that the shorting is just reverse to ksort() function.
i.e sorting associative arrays in descending order by keys .