The sort() array function is used to align the arrays in the ascending alphabetical order.
Example
[php]$cars = array("Volvo", "BMW", "Toyota");
sort($cars);
$clength = count($cars);
for($x = 0; $x < $clength; $x++) {
echo $cars[$x];
echo "<br>";
}
?>[/php]
In the above code, we have disclosed the general array function. Then on the next line, we are instructing the php script to sort the array values in ascending order.
We have created one more variable called $clength to count the number of arrays value. This is essential because without the count you can run the next step. It is required to disclose the count from which you will be increasing the value.
Then we are using for loop statement. We have mentioned the value of $x is less than the $clength total. It means the script will start from the zero and end at the top array count. At the end, we are using increment operator to instruct to increase the value by one.
In the echo statement, we are using $cars[$x] statement to pull the value from the array and increment it in ascending order.
The block of code will show result.
BMW
Toyota
Volvo
2) Sorting the numbers in descending numerical order
Example
[php]<?php
$numbers = array(4, 6, 2, 22, 11);
rsort($numbers);
$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
echo $numbers[$x];
echo "<br>";
}
?>[/php]
In the above script we have change the sort value to rsort which states the decending order of the arrays value. The above will generate following result.
22
11
6
4
2
3) Sort Array (Ascending Order), According to Value – asort()
Now we are sorting the array value in the ascending order on the basis of the value figure.
Example
[php]<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($age);
foreach($age as $x => $value) {
echo "Key " . $x . ", Value " . $value;
echo "<br>";
}
?>[/php]
In the above block of code, we are using a sort($age) function to tell php script to sort the value in the ascending order. Later we are using a foreach statement to generate the result. In the foreach statement, we are stating the $age will have the value of the $x. By doing this we are passing the array value to the $x which represent the key and the $value represent the value in the $age array.
In the echo statement, we are getting $x and $value from the above foreach statement. The output of the above code is given below.
Key Peter, Value 35
Key Ben, Value 37
Key Joe, Value 43
4) Sort Array (Ascending Order), According to Key – ksort()
This statement sorts the associative arrays in the ascending order according to the key.
Example
[php]<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
ksort($age);
foreach($age as $x => $value) {
echo "Key " . $x . ", Value " . $value;
echo "<br>";
}
?>[/php]
In the above statement, the script is instructed to use the key from the array and sort them in the ascending alphabetically order.
The result of the statement will look like this.
Key Ben, Value 37
Key Joe, Value 43
Key Peter, Value 35