PHP Tutorial
What is PHP Environment Setup of PHP PHP Syntax PHP Comments PHP Echo & print PHP Variables PHP Scope Variables PHP $ and $$ Variable PHP Constants PHP Operators PHP Data TypesPHP Conditional Statements
PHP Loop Type
PHP Arrays
PHP String PHP FunctionsPHP Form Examples
Form Intro Get & Post Form Validation / Required PHP Complete FormPHP Advanced
PHP Include / Require PHP Date & Time PHP Session PHP Cookies PHP File Handling PHP Open File PHP Read File PHP Write File PHP Append File PHP Delete File PHP File Upload PHP Sending Mail PHP Error HandlingSorting Arrays
The elements in an array can be sorted in alphabetical or numerical order, descending or ascending.
PHP - Sort Functions For Arrays
In this chapter, we will go through the following PHP array sort functions:
sort()
- sort arrays in ascending orderrsort()
- sort arrays in descending orderasort()
- sort associative arrays in ascending order, according to the valueksort()
- sort associative arrays in ascending order, according to the keyarsort()
- sort associative arrays in descending order, according to the valuekrsort()
- sort associative arrays in descending order, according to the key
Sort Array in Ascending Order – sort()
The following function sorts the elements of a numerical array in ascending numerical order:
Example:
<?php
$numbers = array(40, 61, 2, 22, 13);
sort($numbers);
$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
echo $numbers[$x];
echo "<br>";
}
?>
Output:
2
13
22
40
61
Sort Array in Descending Order – rsort()
The following function sorts the elements of a numerical array in descending numerical order:
Example:
<?php
$numbers = array(40, 61, 2, 22, 13);
rsort($numbers);
$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
echo $numbers[$x];
echo "<br>";
}
?>
Output:
61
40
22
13
2
Sort Array in Ascending Order,According to Value – asort()
The following function sorts an associative array in ascending order, according to the value:
Example:
<?php
$age = array("ayush"=>"23", "shankar"=>"47", "kailash"=>"41");
asort($age);
foreach($age as $x => $x_value) {
echo "Key = " . $x . ", Value = " . $x_value;
echo "<br>";
}
?>
Output:
Key = ayush, Value = 23
Key = kailash, Value = 41
Key = shankar, Value = 47
Sort Array in Ascending Order, According to Key – ksort()
The following function sorts an associative array in ascending order, according to the key:
Example:
<?php
$age = array("ranjeet"=>"23", "hemant"=>"47", "kailash"=>"41");
ksort($age);
foreach($age as $x => $x_value) {
echo "Key = " . $x . ", Value = " . $x_value;
echo "<br>";
}
?>
Output:
Key = hemant, Value = 47
Key = kailash, Value = 41
Key = ranjeet, Value = 23
Sort Array in Descending Order, According to Value – arsort()
The following function sorts an associative array in descending order, according to the value.
Example:
<?php
$age = array("hemant"=>"23", "ranjeet"=>"47", "kailash"=>"41");
arsort($age);
foreach($age as $x => $x_value) {
echo "Key = " . $x . ", Value = " . $x_value;
echo "<br>";
}
?>
Output:
Key = ranjeet, Value = 47
Key = kailash, Value = 41
Key = hemant, Value = 23
Sort Array in Descending Order, According to Key – krsort()
The following function sorts an associative array in descending order, according to the key.
Example:
<?php
$age = array("ranjeet"=>"23", "hemant"=>"47", "kailash"=>"41");
krsort($age);
foreach($age as $x => $x_value) {
echo "Key = " . $x . ", Value = " . $x_value;
echo "<br>";
}
?>
Output:
Key = ranjeet, Value = 23
Key = kailash, Value = 41
Key = hemant, Value = 47
Elevenstech Web Tutorials
Elevenstech Web Tutorials helps you learn coding skills and enhance your skills you want.
As part of Elevenstech's Blog, Elevenstech Web Tutorials contributes to our mission of “helping people learn coding online”.
Read More
Newsletter
Subscribe to get the latest updates from Elevenstech Web Tutorials and stay up to date