Associative array, Shorting Arrays and Array data structure
An associative array, Shorting Arrays and Array data structure
Associative array Shorting Arrays and Array data structure:-
therefore short array in ascending or descending order and array element can be stored in numerical or alphabetical order
Sort Functions For Arrays in PHP

First of all, In this PHP Shorting Arrays, we will introduce the following PHP array sort functions:
- sort() – It used to sort arrays in ascending order
rsort()– Sort arrays in descending orderasort()– as a result sort associative arrays in ascending order, according to the valueksort()– In Program sort associative arrays in ascending order, according to the keyarsort()-this function we can be used to sort associative arrays in descending order, according to the valuekrsort()-also used to sort associative arrays in descending order, according to the key
Sort Array in Ascending Order – sort()
This example sorts the elements of the $ colors array in ascending alphabetical order:
|
1 2 3 4 5 6 7 8 9 10 |
<?php $colors = array("Blue", "Yellow", "Green"); sort($colors); $clength = count($colors); for($x = 0; $x < $clength; $x++) { echo $colors[$x]; echo "<br>"; } ?> |
Finally Output:-
|
1 2 3 |
Blue Green Yellow |
Another Example
|
1 2 3 4 5 6 7 8 9 10 |
<?php $numbers = array(9, 3, 7, 29, 4); sort($numbers); $arrlength = count($numbers); for($x = 0; $x < $arrlength; $x++) { echo $numbers[$x]; echo "<br>"; } ?> |
Sort Array in Descending Order – rsort()
Therefore $cars array in descending alphabetical order:
|
1 2 3 4 5 6 7 8 9 10 |
<?php $car = array("Volvo", "BMW", "Toyota"); rsort($car); $clength = count($car); for($x = 0; $x < $clength; $x++) { echo $car[$x]; echo "<br>"; } ?> |
Finally Output:
|
1 2 3 |
Volvo Toyota BMW |
Resource
PHP.net