The sort () function is used to sort the array cells from low to high.
The Rsort () function is used to sort the array cells from high to low.
The Asort () function is used to sort the array cells from low to high and keep the index relationship.
The Arsort () function is used to sort the array cells from high to low and to maintain an indexed relationship.
The Ksort () function is used to sort the array cells by their key names from low to high.
The Krsort () function is used to sort the array cells by their key names from high to low.
Sort ()
The PHP sort () function is used to sort the array cells from low to high, and returns TRUE if successful, and FALSE if they fail.
Note: This function assigns a new key name to the cells in the sorted array, which deletes the original key name and not just the reordering.
Grammar:
BOOL Sort (array &array [, int sort_flags])
Optional parameter sort_flags the behavior used to change the sort:
Sort_flags Value Description
Sort_regular Normal comparison unit
Sort_numeric units are compared as numbers.
Sort_string units are compared as strings
Sort_locale_string the cell as a string based on the current region (LOCALE) setting
Example:
$arr = Array ("B", "A", "C");
Sort ($arr);
Print_r ($arr);
?>
Run the example output:
Array ([0] => a [1] => b [2] => c)
In this case, the $arr array cells are sorted alphabetically, and the array cells are sorted, and the key values are reassigned.
Rsort ()
the PHP rsort () function behaves in contrast to sort (), and the array cells are sorted from highest to lowest, refer to the sort () function.
Asort ()
the PHP asort () function is used to sort the array cells from low to high and keep the index relationship, and returns TRUE if successful, and FALSE if it fails.
Grammar:
BOOL Asort (array &array [, int sort_flags])
Optional parameters sort_flags The behavior of changing the sort, see sort ().
Example:
$arr = Array ("B", "A", "C");
Asort ($arr);
Print_r ($arr);
?>
Run the example output:
Array ([1] => a [0] => b [2] => c)
Arsort ()
the PHP arsort () function behaves in contrast to the Asort (), and the array unit is sorted from highest to lowest and maintains an indexed relationship, refer to the Asort () function.
Ksort ()
the PHP ksort () function is used to sort the array cells by their key names from low to high, and returns TRUE if successful, and FALSE if they fail.
This function retains the original key name and is therefore often used in associative arrays.
Grammar:
BOOL Ksort (array &array [, int sort_flags])
Optional parameters sort_flags The behavior of changing the sort, see sort ().
Example:
$arr = Array ("B" =>18, "a" =>20, "C" =>25);
Ksort ($arr);
Print_r ($arr);
?>
Run the example output:
Array ([a] => [b] => [C] => 25)
Krsort ()
the PHP krsort () function behaves in contrast to the Ksort (), which is sorted by the key name from high to low, refer to the Ksort () function.