PHP function, PHP sorting function, PHP array function, PHP array sorting function, sort, asort, rsort, krsort, ksort
The sort () function is used to sort array units from low to high.
The rsort () function is used to sort array units from high to low.
The asort () function is used to sort array units from low to high and maintain the index relationship.
Arsort () is used to sort array units from high to low and maintain the index relationship.
The ksort () function is used to sort array units by key names from low to high.
The krsort () function is used to sort array units by key names from high to low.
Sort ()
The PHP sort () function is used to sort array units from low to high. If the array unit is successfully sorted, TRUE is returned. If the array unit fails, FALSE is returned.
Note: This function will assign a new key name to the unit in the sorted array, which will delete the original key name, not just the reorder.
Syntax:
Bool sort (array & array [, int sort_flags])
The optional parameter sort_flags is used to change the sorting behavior:
Sort_flags value description
SORT_REGULAR normal comparison unit
The SORT_NUMERIC unit is compared as a number.
The SORT_STRING unit is compared as a string.
SORT_LOCALE_STRING compare the unit as a string based on the current region (locale) settings
Example:
$ Arr = array ("B", "a", "c ");
Sort ($ arr );
Print_r ($ arr );
?>
Run the sample output:
Array ([0] => a [1] => B [2] => c)
In this example, the $ arr array units are sorted alphabetically, and the array units are sorted and the key values are re-allocated.
Rsort ()
The PHP rsort () function is opposite to sort (). sort the array units in descending order. For more information, see sort () function.
Asort ()
The PHP asort () function is used to sort array units from low to high and maintain the index relationship. if it succeeds, TRUE is returned, and FALSE is returned if it fails.
Syntax:
Bool asort (array & array [, int sort_flags])
The optional parameter sort_flags is used to change the sorting behavior. for details, see sort ().
Example:
$ Arr = array ("B", "a", "c ");
Asort ($ arr );
Print_r ($ arr );
?>
Run the sample output:
Array ([1] => a [0] => B [2] => c)
Arsort ()
The PHP arsort () function is opposite to asort (). It sorts array units from high to low and maintains the index relationship. For more information, see use the asort () function.
Ksort ()
The PHP ksort () function is used to sort array cells by key names from low to high. if the key name is successful, TRUE is returned. if the key name fails, FALSE is returned.
This function retains the original key name, so it is often used to associate arrays.
Syntax:
Bool ksort (array & array [, int sort_flags])
The optional parameter sort_flags is used to change the sorting behavior. for details, see sort ().
Example:
$ Arr = array ("B" => 18, "a" => 20, "c" => 25 );
Ksort ($ arr );
Print_r ($ arr );
?>
Run the sample output:
Array ([a] => 20 [B] => 18 [c] => 25)
Krsort ()
The behavior of the PHP krsort () function is opposite to that of ksort (). sort the array units by key name from high to low. for details, refer to the ksort () function.