Core PHP

Array Sort Functions

Array Sort Functions
array_multisort()
bool array_multisort(array $qaArray1, int $iArray1SortOrder = SORT_ASC, int $iArray1SortFlags = SORT_REGULAR, ...)
This function sorts an array or a several arrays at once. Each array can be passed in as a triple of the array, the sort order, and the sort flags. The sort order and sort flags are optional and can be omitted to use the default sort values. The function returns true if the sort is successful and false if it is not.
Argument Description
array $qaArray1 This is an array that is to be sorted. There may be several.
int $iArray1SortOrder This specifies the sort order for the array and can be specified as one of two values: SORT_ASC or SORT_DESC.
int $iArray1SortFlags This specifies how the sort and can be specified as one of the following values:
  • SORT_REGULAR: This compares values normally without changing them.
  • SORT_NUMERIC: This compares the values according to their numeric value.
  • SORT_STRING: This compares the values as strings.
  • SORT_LOCALE_STRING: This compares the values as string according to the current locale.
  • SORT_NATURAL: This compares the values using natural ordering, similar to natsort().
  • SORT_FLAG_CASE: This can be combined with SORT_STRING or SORT_LOCALE_STRING via bitwise or to make the sort case insensitive.
arsort()
bool arsort(array &$qarArray, int $iArray1SortFlags = SORT_REGULAR)
This function sorts, in reverse order, the values of the array $qarArray, according to the comparison specified by $iArray1SortFlags. The function returns true if the sort is successful and false if it is not.
Argument Description
array $qaArray This is an array that is to be sorted.
int $iArray1SortFlags This specifies how the sort and can be specified as one of the following values:
  • SORT_REGULAR: This compares values normally without changing them.
  • SORT_NUMERIC: This compares the values according to their numeric value.
  • SORT_STRING: This compares the values as strings.
  • SORT_LOCALE_STRING: This compares the values as string according to the current locale.
  • SORT_NATURAL: This compares the values using natural ordering, similar to natsort().
  • SORT_FLAG_CASE: This can be combined with SORT_STRING or SORT_LOCALE_STRING via bitwise or to make the sort case insensitive.
asort()
bool asort(array &$qarArray, int $iArray1SortFlags = SORT_REGULAR)
This function sorts the values of the array $qarArray, according to the comparison specified by $iArray1SortFlags. The function returns true if the sort is successful and false if it is not.
Argument Description
array &$qarArray This is an array that is to be sorted.
int $iArray1SortFlags This specifies how the sort and can be specified as one of the following values:
  • SORT_REGULAR: This compares values normally without changing them.
  • SORT_NUMERIC: This compares the values according to their numeric value.
  • SORT_STRING: This compares the values as strings.
  • SORT_LOCALE_STRING: This compares the values as string according to the current locale.
  • SORT_NATURAL: This compares the values using natural ordering, similar to natsort().
  • SORT_FLAG_CASE: This can be combined with SORT_STRING or SORT_LOCALE_STRING via bitwise or to make the sort case insensitive.
krsort()
bool krsort(array &$qarArray, int $iArray1SortFlags = SORT_REGULAR)
This function sorts, in reverse order, the entries of the array $qarArray based on the keys, according to the comparison specified by $iArray1SortFlags. The function returns true if the sort is successful and false if it is not.
Argument Description
array &$qarArray This is an array that is to be sorted.
int $iArray1SortFlags This specifies how the sort and can be specified as one of the following values:
  • SORT_REGULAR: This compares values normally without changing them.
  • SORT_NUMERIC: This compares the values according to their numeric value.
  • SORT_STRING: This compares the values as strings.
  • SORT_LOCALE_STRING: This compares the values as string according to the current locale.
  • SORT_NATURAL: This compares the values using natural ordering, similar to natsort().
  • SORT_FLAG_CASE: This can be combined with SORT_STRING or SORT_LOCALE_STRING via bitwise or to make the sort case insensitive.
ksort()
bool ksort(array &$qarArray, int $iArray1SortFlags = SORT_REGULAR)
This function sorts the entries of the array $qarArray based on the keys, according to the comparison specified by $iArray1SortFlags. The function returns true if the sort is successful and false if it is not.
Argument Description
array &$qarArray This is an array that is to be sorted.
int $iArray1SortFlags This specifies how the sort and can be specified as one of the following values:
  • SORT_REGULAR: This compares values normally without changing them.
  • SORT_NUMERIC: This compares the values according to their numeric value.
  • SORT_STRING: This compares the values as strings.
  • SORT_LOCALE_STRING: This compares the values as string according to the current locale.
  • SORT_NATURAL: This compares the values using natural ordering, similar to natsort().
  • SORT_FLAG_CASE: This can be combined with SORT_STRING or SORT_LOCALE_STRING via bitwise or to make the sort case insensitive.
natcasesort()
bool natcasesort(array &$qarArray, int $iArray1SortFlags = SORT_REGULAR)
This function sorts the values of the array $qarArray, according to a case-insensitive natural ordering. The function returns true if the sort is successful and false if it is not.
Argument Description
array &$qarArray This is an array that is to be sorted.
natsort()
bool natsort(array &$qarArray, int $iArray1SortFlags = SORT_REGULAR)
This function sorts the values of the array $qarArray, according to a natural ordering. The function returns true if the sort is successful and false if it is not.
Argument Description
array &$qarArray This is an array that is to be sorted.
rsort()
bool rsort(array &$qarArray, int $iArray1SortFlags = SORT_REGULAR)
This function sorts, in reverse order, the values of the array $qarArray, according to the comparison specified by $iArray1SortFlags. The function returns true if the sort is successful and false if it is not. Unlike arSort(), this does not maintain the key relationship.
Argument Description
array $qaArray This is an array that is to be sorted.
int $iArray1SortFlags This specifies how the sort and can be specified as one of the following values:
  • SORT_REGULAR: This compares values normally without changing them.
  • SORT_NUMERIC: This compares the values according to their numeric value.
  • SORT_STRING: This compares the values as strings.
  • SORT_LOCALE_STRING: This compares the values as string according to the current locale.
  • SORT_NATURAL: This compares the values using natural ordering, similar to natsort().
  • SORT_FLAG_CASE: This can be combined with SORT_STRING or SORT_LOCALE_STRING via bitwise or to make the sort case insensitive.
shuffle()
bool shuffle(array &$qarArray)
This function randomizes the order of the values of the array $qarArray. The function returns true if it is successful and false if it is not.
Argument Description
array &$qarArray This is an array that is to be sorted.
sort()
bool sort(array &$qarArray, int $iArray1SortFlags = SORT_REGULAR)
This function sorts the values of the array $qarArray, according to the comparison specified by $iArray1SortFlags. The function returns true if the sort is successful and false if it is not. Unlike asort(), this does not maintain the key relationship.
Argument Description
array &$qarArray This is an array that is to be sorted.
int $iArray1SortFlags This specifies how the sort and can be specified as one of the following values:
  • SORT_REGULAR: This compares values normally without changing them.
  • SORT_NUMERIC: This compares the values according to their numeric value.
  • SORT_STRING: This compares the values as strings.
  • SORT_LOCALE_STRING: This compares the values as string according to the current locale.
  • SORT_NATURAL: This compares the values using natural ordering, similar to natsort().
  • SORT_FLAG_CASE: This can be combined with SORT_STRING or SORT_LOCALE_STRING via bitwise or to make the sort case insensitive.
uasort()
bool uasort(array &$qarArray, function $fnCompareValueFn)
This function sorts the values of the array $qarArray, according to the comparison specified by the comparison function $fnCompareValueFn. The keys are preserved from the original array. The function returns true, if it is successfully sorted and false if it is not.
Argument Description
array &$qarArray This is the array that is sorted.
function $fnCompareValueFn This a comparison function that returns an integer value that is greater than zero, zero, or less than zero to indicate that the first value is less than, equal to, or greater than the second, respectively.
uksort()
bool uksort(array &$qarArray, function $fnCompareKeyFn)
This function sorts the values of the array $qarArray, according to the keys and the comparison specified by the comparison function $fnCompareKeyFn. The keys are preserved from the original array. The function returns true, if it is successfully sorted and false if it is not.
Argument Description
array &$qarArray This is the array that is sorted.
function $fnCompareKeyFn This a comparison function that returns an integer value that is greater than zero, zero, or less than zero to indicate that the first value is less than, equal to, or greater than the second, respectively.
usort()
bool usort(array &$qarArray, function $fnCompareValueFn)
This function sorts the values of the array $qarArray, according to the comparison specified by the comparison function $fnCompareValueFn. All of the keys from the original array are removed and replaced with numerical keys based on the new order of the elements. The function returns true, if it is successfully sorted and false if it is not.
Argument Description
array &$qarArray This is the array that is sorted.
function $fnCompareValueFn This a comparison function that returns an integer value that is greater than zero, zero, or less than zero to indicate that the first value is less than, equal to, or greater than the second, respectively.
 
 

© 2007–2025 XoaX.net LLC. All rights reserved.