Sorting Arrays in PHP 5

14 Nov, 2017
What is sorting?Sorting refers to ordering data in an alphabetical, numerical order and increasing or decreasing fashion according to some linear relationship among the data items.Sorting greatly improves the efficiency of searching.
Sorting Functions For Arrays In PHP
sort() – sorts arrays in ascending orderrsort() – sorts arrays in descending orderasort() – sorts associative arrays in ascending order, according to the valueksort() – sorts associative arrays in ascending order, according to the keyarsort() – sorts associative arrays in descending order, according to the valuekrsort() – sorts associative arrays in descending order, according to the key
sort() – sorts arrays in ascending order
rsort() – sorts arrays in descending order
asort() – sorts associative arrays in ascending order, according to the value
ksort() – sorts associative arrays in ascending order, according to the key
arsort() – sorts associative arrays in descending order, according to the value
krsort() – sorts 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:
INPUT :
<!DOCTYPE html><html><body>  <?php$numbers = array(40, 61, 2, 22, 13);sort($numbers);  $arrlength = count($numbers);for($x = 0; $x < $arrlength; $x++) {    echo $numbers[$x];    echo "<br>";}?>  </body></html>
OUTPUT :
213224061
Sort Array in Descending Order – rsort()The following function sorts the elements of a numerical array in descending numerical order:
INPUT :
<!DOCTYPE html><html><body>  <?php$numbers = array(40, 61, 2, 22, 13);rsort($numbers);  $arrlength = count($numbers);for($x = 0; $x < $arrlength; $x++) {    echo $numbers[$x];    echo "<br>";}?>  </body></html>
OUTPUT :
614022132
Sort Array in Ascending Order,According to Value – asort()The following function sorts an associative array in ascending order, according to the value:
INPUT :
<!DOCTYPE html><html><body>  <?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>";}?>  </body></html>
OUTPUT :
Key=Ayush, Value=23Key=Kailash, Value=41Key=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:
INPUT :
<!DOCTYPE html><html><body>  <?php$age = array("ayush"=>"23", "shankar"=>"47", "kailash"=>"41");ksort($age);  foreach($age as $x => $x_value) {    echo "Key=" . $x . ", Value=" . $x_value;    echo "<br>";}?>  </body></html>
OUTPUT :
Key=Ayush, Value=23Key=Kailash, Value=41Key=Shankar, Value=47
Sort Array in Descending Order, According to Value – arsort()The following function sorts an associative array in descending order, according to the value.
INPUT :
<!DOCTYPE html><html><body>  <?php$age = array("ayush"=>"23", "shankar"=>"47", "kailash"=>"41");arsort($age);  foreach($age as $x => $x_value) {    echo "Key=" . $x . ", Value=" . $x_value;    echo "<br>";}?>  </body></html>
OUTPUT :
Key=Shankar, Value=47Key=Kailash, Value=41Key=Ayush, 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.
INPUT :
<!DOCTYPE html><html><body>  <?php$age = array("ayush"=>"23", "shankar"=>"47", "kailash"=>"41");krsort($age);  foreach($age as $x => $x_value) {    echo "Key=" . $x . ", Value=" . $x_value;    echo "<br>";}?>  </body></html>
OUTPUT :
Key=Shankar, Value=47Key=Kailash, Value=41Key=Ayush, Value=23
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
How to Upload Image into Database and Display it using PHP ?
How to check whether an array is empty using PHP?
PHP | Converting string to Date and DateTime
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?