In this tutorial, you will be learning array sorting functions in PHP. As the name suggests, these functions are used to sort an array alphabetically either in the reverse order or forward order, according to the key etc.
All the array sorting functions used in php are listed below.
- sort()
- rsort()
- asort()
- arsort()
- ksort()
- krsort()
- array_multisort()
- natsort()
- natcasesort()
- usort()
- uasort()
- uksort()
- shuffle()
Out of these, we have described below five arrays functions namely sort(), asort(), arsort(), ksort() and krsort(). The functioning of all the above functions are different but their syntaxes are almost similar.
SORT() FUNCTION
This function is used to sort an array in alphabetical order.
Syntax : sort($arr, $flags);
Two parameters are used in this function which are described below.
- $arr- This is the input array whose elements are to be sorted.
- $flags- This is an additional integer parameter that can have following constant values according to the modifications you desire.
(1) SORT_REGULAR
(2) SORT_STRING
(3) SORT_NUMERIC
(4) SORT_NATURAL
(5) SORT_FLAG_CASE
(6) SORT_LOCALE_STRING
Result/ Return Value : On the successful execution of the php script, TRUE will be returned. And it will return FALSE if the function fails to do so.
Example : We have shown below an example of sort() function.
Output : You will get the following output after running the above php script.
ASORT() FUNCTION
This function is used to sort an array of elements without affecting its associative indices.
Syntax : asort($arr, $flags);
The parameters used in this function are similar to the sort() function.
Result/ Return Value : On successful execution, TRUE will be returned. Otherwise, it will return FALSE.
Example : An example of asort() function is shown below.
Output : The following output will be generated after the successful execution of the php script.
ARSORT() FUNCTION
The function arsort() sorts the array in the reverse alphabetical order without affecting its associative indices.
Syntax : arsort($arr, $flags);
Result/ Return Value : If the script is successfully executed, TRUE is returned. Otherwise, FALSE is returned.
Example : Have a look at the below example of function arsort().
Output : The output will look something like this. KSORT() FUNCTION
This function is used to sort an associative array according to the alphabetical order of the keys.
Syntax : ksort($arr, $flags);
Result/ Return Value : TRUE is returned if script runs successfully. Else, FALSE will be returned.
Example : Have a look at the below example of function arsort(). Output : The following output will be generated after running the above php codes.
KRSORT() FUNCTION
It is similar to the ksort() function. However, it sorts the associative array in the reverse alphabetical order of the keys.
Syntax : krsort($arr, $flags);
Result/ Return Value : It will return TRUE if the php script runs successfully. Otherwise, FALSE will be returned.
Example : An example of krsort() function is shown below.
Output : You will get the following output after running the above php script.
We have provided you the information about some of the php function which are used to sort an array in different manners for different purposes. Hope you liked this article. For more updates and related information, keep visiting our php blogs.