PHP Basic Tutorial
MySQL Connection
PHP provides several functions to sort arrays. You can sort arrays by their values or keys, in ascending or descending order, and you can also use custom sorting functions.
sort()
- sorts the array in ascending orderrsort()
- sorts the array in descending orderasort()
- sorts the array in ascending order while maintaining the key-value pairsksort()
- sorts the array by key in ascending orderarsort()
- sorts the array in descending order while maintaining the key-value pairskrsort()
- sorts the array by key in descending orderThe following example sorts the elements of the $fruits
array in ascending alphabetical order:
Try it yourself
The following example sorts the elements of the $numbers
array in ascending numerical order:
Try it yourself
When using the sort()
function in PHP to sort an array containing both numerical and alphabetical values, it sorts the elements lexicographically (alphabetically by character values).
Try it yourself
The following example sorts the elements of the $fruits
array in descending alphabetical order:
Try it yourself
The following example sorts the elements of the $numbers
array in descending numerical order:
Try it yourself
The following example sorts the array in ascending order while maintaining the key-value pairs.
Try it yourself
The following example sorts the array in descending order while maintaining the key-value pairs.
Try it yourself
The following example sorts the array by key in ascending order while maintaining the key-value pairs.
Try it yourself
The following example sorts the array by key in descending order while maintaining the key-value pairs.
Try it yourself