PHP Array sort


  • The sort() function sorts the array elements from low to high.
  • The rsort() function sorts the array elements from highest to lowest.
  • The asort() function sorts the array elements from low to high and maintains the index relation.
  • The arsort() function sorts the array elements from high to low and maintains the index relation.
  • The ksort() function is used to sort the array elements from lowest to highest key names.
  • The krsort() function is used to sort the array elements from highest to lowest key names.

sort()

The PHP sort() function sorts the array elements from low to high, returns TRUE if successful, and FALSE if they fail.

Note:This function assigns a new key name to the cells in the sorted array, which will remove the old key name and not just the reordering.

grammar:

bool sort(array & array [, int sort_flags])

The optional parameter sort_flags is used to change the sorting behavior:

sort_flags value Description
SORT_REGULAR Normal comparison unit
SORT_NUMERIC The element is compared as a number
SORT_STRING The element is compared as a string
SORT_LOCALE_STRING Compares a cell as a string based on the current locale setting

example:

$arr = array(" b ", " a ", " c ");
sort($arr);
print_r($arr);

Run the example output:

array [0] => a [1] => b [2] => c)

In this example, the $arr array cells are sorted alphabetically, and the array elements are sorted, and the keys are reassigned.


rsort()

The PHP rsort() function behaves in the reverse order of sort(), sorting the array elements from high to low. Please refer to The sort()function.


asort()

PHP asort() function is used to sort the array elements from low to high and maintain the index relationship, if successful, returns TRUE, failure returns FALSE.

grammar:

bool asort(array & array [, int sort_flags])

The optional sort_flags parameter is used to change the sorting behavior, see sort().

example:

$arr = array(" b ", " a ", " c ");
asort($arr);
print_r($arr);

Run the example output:

array [1] => a [0] => b [2] => c)

arsort()

PHP arsort() function and asort() on the contrary, the array elements from high to low sort and maintain the index relationship, please refer to asort() function.


ksort()

The PHP ksort() function sorts the array elements from low to high, returns TRUE if successful, and FALSE if not.

This function will retain the original key name, it is often used in associative array.

grammar:

bool ksort(array & array [, int sort_flags])

The optional sort_flags parameter is used to change the sorting behavior, see sort().

example:

$arr = array(" b " => 18, " a " => 20, " c " => 25);
ksort($arr);
print_r($arr);

Run the example output:

Array ( 
  [a] => 20 
  [b] => 18 
  [c] => 25
)

krsort()

The PHP krsort() function behaves in the reverse order of ksort(), sorting the array elements from high to low according to the key names. Please refer to the ksort() function.




Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook