Sorting functions in PHP Sort, asort, Rsort, Krsort, ksort difference analysis _php Techniques

Source: Internet
Author: User

The sort () function is used to sort the array cells from low to high.
The Rsort () function is used to sort the array cells from high to low.
The Asort () function is used to sort the array cells from low to high and keep the index relationship.
The Arsort () function is used to sort the array cells from high to low and to maintain an indexed relationship.
The Ksort () function is used to sort the array cells by their key names from low to high.
The Krsort () function is used to sort the array cells by their key names from high to low.

Sort ()

The PHP sort () function is used to sort the array cells from low to high, and 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 deletes the original key name and not just the reordering.
Grammar:
BOOL Sort (array &array [, int sort_flags])
Optional parameter sort_flags the behavior used to change the sort:
Sort_flags Value Description
Sort_regular Normal comparison unit
Sort_numeric units are compared as numbers.
Sort_string units are compared as strings
Sort_locale_string the cell as a string based on the current region (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 case, the $arr array cells are sorted alphabetically, and the array cells are sorted, and the key values are reassigned.

Rsort ()
the PHP rsort () function behaves in contrast to sort (), and the array cells are sorted from highest to lowest, refer to the sort () function.

Asort ()
the PHP asort () function is used to sort the array cells from low to high and keep the index relationship, and returns TRUE if successful, and FALSE if it fails.
Grammar:
BOOL Asort (array &array [, int sort_flags])
Optional parameters sort_flags The behavior of changing the sort, 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 ()
the PHP arsort () function behaves in contrast to the Asort (), and the array unit is sorted from highest to lowest and maintains an indexed relationship, refer to the Asort () function.

Ksort ()
the PHP ksort () function is used to sort the array cells by their key names from low to high, and returns TRUE if successful, and FALSE if they fail.
This function retains the original key name and is therefore often used in associative arrays.
Grammar:
BOOL Ksort (array &array [, int sort_flags])
Optional parameters sort_flags The behavior of changing the sort, see sort ().
Example:
  
  

$arr = Array ("B" =>18, "a" =>20, "C" =>25);
Ksort ($arr);
Print_r ($arr);
?>

Run the example output:

Array ([a] => [b] => [C] => 25)

  Krsort ()
the PHP krsort () function behaves in contrast to the Ksort (), which is sorted by the key name from high to low, refer to the Ksort () function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.