PHP Sorting Arrays
In PHP, sorting arrays involves rearranging the elements of an array based on a specific order, such as ascending (low to high) or descending (high to low).
• PHP - Sort Functions For Arrays
sort() - sort arrays in ascending order
rsort() - sort arrays in descending order
asort() - sort associative arrays in ascending order, according to the value
ksort() - sort associative arrays in ascending order, according to the key
arsort() - sort associative arrays in descending order, according to the value
krsort() - sort associative arrays in descending order, according to the key
• Sort Array in Ascending Order - sort()
The following example sorts the elements of the $cars array in ascending alphabetical order:
Example
Remove the second item:
<!DOCTYPE html> <html> <body> <?php $cars = array("Volvo", "BMW", "Toyota"); sort($cars); $clength = count($cars); for($x = 0; $x < $clength; $x++) { echo $cars[$x]; echo "
"; } ?> </body> </html>
Output
BMW
Toyota
Volvo
• Sort Array in Descending Order - rsort()
The following example sorts the elements of the $cars array in descending alphabetical order:
Example
<!DOCTYPE html> <html> <body> <?php $cars = array("Volvo", "BMW", "Toyota"); rsort($cars); $clength = count($cars); for($x = 0; $x < $clength; $x++) { echo $cars[$x]; echo "
"; } ?> </body> </html>
Output
Volvo
Toyota
BMW
• Sort Array (Ascending Order), According to Key - ksort()
The following example sorts an associative array in ascending order, according to the key:
Example
<!DOCTYPE html> <html> <body> <?php $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); ksort($age); foreach($age as $x => $x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "
"; } ?> </body> </html>
Output
Key=Ben, Value=37
Key=Joe, Value=43
Key=Peter, Value=35
• Sort Array (Ascending Order), According to Value - asort()
The following example sorts an associative array in ascending order, according to the value:
Example
Remove the "model":
<!DOCTYPE html> <html> <body> <?php $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); asort($age); foreach($age as $x => $x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "
"; } ?> </body> </html>
Output
Key=Peter, Value=35
Key=Ben, Value=37
Key=Joe, Value=43
• Sort Array (Descending Order), According to Value - arsort()
The following example sorts an associative array in descending order, according to the value:
Example
<!DOCTYPE html> <html> <body> <?php $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); arsort($age); foreach($age as $x => $x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "
"; } ?> </body> </html>
Output
Key=Joe, Value=43
Key=Ben, Value=37
Key=Peter, Value=35
• Sort Array (Descending Order), According to Key - krsort()
The following example sorts an associative array in descending order, according to the key:
Example
<!DOCTYPE html> <html> <body> <?php $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); krsort($age); foreach($age as $x => $x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "
"; } ?> </body> </html>
Output
Key=Peter, Value=35
Key=Joe, Value=43
Key=Ben, Value=37