Home > Article > Backend Development > How to use php sort function
php's sort function is used to sort numerical arrays in ascending order. The syntax of this function is "sort(array, sortingtype);", where the parameter array represents the array to be sorted, and the parameter "sortingtype" represents How to arrange the elements or items of an array.
Recommended: "PHP Video Tutorial"
sort() function sorts a numerical array in ascending order.
Tip: Please use the rsort() function to sort the numeric array in descending order.
Syntax
sort(array,sortingtype);
Parameters
array required. Specifies the array to be sorted.
sortingtype Optional. Specifies how the elements/items of an array are arranged. Possible values:
0 = SORT_REGULAR - Default. Put each item in regular order (Standard ASCII, don't change the type).
1 = SORT_NUMERIC - treat each item as a number.
2 = SORT_STRING - Treat each item as a string.
3 = SORT_LOCALE_STRING - Treat each item as a string, based on the current locale (can be changed via setlocale()).
4 = SORT_NATURAL - Treat each item as a string, using natural sorting like natsort().
5 = SORT_FLAG_CASE - Can be combined (bitwise ORed) with SORT_STRING or SORT_NATURAL to sort strings, case-insensitively.
Technical Details
Return value: TRUE if successful, FALSE if failed.
PHP version: 4
More examples
Example 1
Sort the elements in the array $numbers in ascending order by number:
<?php $numbers=array(4,6,2,22,11); sort($numbers); ?>
Run result:
2 4 6 11 22
The above is the detailed content of How to use php sort function. For more information, please follow other related articles on the PHP Chinese website!