Skip to content

Array Sorting Functions In PHP

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.

  1. sort()
  2. rsort()
  3. asort()
  4. arsort()
  5. ksort()
  6. krsort()
  7. array_multisort()
  8. natsort()
  9. natcasesort()
  10. usort()
  11. uasort()
  12. uksort()
  13. 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.

array-php

Example : We have shown below an example of sort() function. 

green-yello-php-example

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.

php-black-screen

Example : An example of asort() function is shown below. 

jassy-peter-php-output

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.

php arsort-1024x473

Example : Have a look at the below example of function arsort(). 

php-output-arsortoutput-1024x490

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.

php-ksort-1024x457
php-ksortoutput-1024x476

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.

php-krsort-1024x462

Example : An example of krsort() function is shown below. 

php-result-krsortoutput-1024x479

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.

Facebook
Twitter
LinkedIn
Pinterest