PHP-sorting functions for arrays
In this section, we will learn the following PHP array sorting functions:
Sort ()-sorts an array in ascending order
Rsort ()-sorts the array in descending order
Asort ()-sorts associative arrays in ascending order based on values
Ksort ()-Sorts the associative array in ascending order, based on the key
Arsort ()-sorts associative arrays in descending order by value
Krsort ()-Sorts the associative array in descending order, based on the key
Sorting an array in ascending order-sort ()
The following examples of Kim are sorted alphabetically by the elements in the array $cars:
Instance
<?php
$cars =array ("Volvo", "BMW", "SAAB");
Sort ($cars);
?>
Running an instance
The following example sorts the elements in an array $numbers in ascending numbers:
Instance
<?php
$numbers =array (3,5,1,22,11);
Sort ($numbers);
?>
Running an instance
Sorting arrays in descending order-rsort ()
The following example sorts the elements in the array $cars in descending alphabetical order:
Instance
<?php
$cars =array ("Volvo", "BMW", "SAAB");
Rsort ($cars);
?>
Running an instance
The following example sorts the elements in an array $numbers in descending order of numbers:
Instance
<?php
$numbers =array (3,5,1,22,11);
Rsort ($numbers);
?>
Running an instance
Sort ascending by value of an array-asort ()
The following example sorts the associative arrays in ascending order based on values:
Instance
<?php
$age =array ("Bill" = "+", "Steve" = "Notoginseng", "Peter" and "43");
Asort.www.bjrongjinhuiyin.com ($age);
?>
Running an instance
Sort ascending by Key array-ksort ()
The following example sorts an associative array in ascending order based on the key:
Instance
<?php
$age =array ("Bill" = "+", "Steve" = "Notoginseng", "Peter" and "43");
Ksort ($age);
?>
Running an instance
Sort Descending by Value array-arsort ()
The following example sorts the associative arrays by value in descending order:
Instance
<?php
$age =array ("Bill" = "+", "Steve" = "Notoginseng", "Peter" and "43");
Arsort ($age);
?>
Running an instance
Sort descending order of keys by Key-Krsort ()
The following example sorts the associative arrays in descending order based on the key:
Instance
<?php
$age =array ("Bill" = "+", "Steve" = "Notoginseng", "Peter" and "43");
Krsort ($age);
?>
PHP Array Sorting