Sorting functions in PHP Sort, asort, Rsort, Krsort, ksort difference analysis, asortrsort_php tutorial

Source: Internet
Author: User
Tags locale setting shuffle

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


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 keep the index relationship.
The Ksort () function is used to sort the array cells from low to high by their key names.
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 it fails.
Note: This function assigns a new key name to the cells in the sorted array, which removes the existing key names and not just the reordering.
Grammar:
BOOL Sort (array &array [, int sort_flags])
The optional parameter sort_flags is used to change the ordering behavior:
Sort_flags Value Description
Sort_regular Normal comparison unit
The Sort_numeric unit is compared as a number.
The sort_string unit is compared as a string.
Sort_locale_string the cell as a string compared to 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 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 (), where the array cells are sorted from high to low, please refer to the sort () function.

Asort ()
The PHP Asort () function is used to sort the array cells from low to high and maintain an index relationship, and if successful returns TRUE, the failure returns FALSE.
Grammar:
BOOL Asort (array &array [, int sort_flags])
The optional parameter sort_flags is used to change the ordering behavior, see sort () in detail.
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 Asort (), where the array cells are sorted from high to low and maintain an index relationship, refer to the Asort () function.

Ksort ()
The PHP Ksort () function is used to sort the array cells by the key name from low to high and returns TRUE if successful, and FALSE if it fails.
This function retains the original key name, so it is often used for associative arrays.
Grammar:
BOOL Ksort (array &array [, int sort_flags])
The optional parameter sort_flags is used to change the ordering behavior, see sort () in detail.
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 Ksort (), and the array cells are sorted from highest to lowest by key name, please refer to the Ksort () function.


Features of various sorts in PHP

Sort by ignoring key names
Sort ()
Rsort ()
Usort ()
Sorting of reserved key names
Asort ()
Arsort ()
Uasort ()
Sort by Key Name
Ksort ()
Krsort ()
Uksort ()
Sort by natural number
Uatsort ()
Uatcasesort ()
Random sort
Shuffle ()
Reverse sort to a new array
Array_reverse ()

Too many things, can only give you the function name, specifically how to use please check the manual

PHP array functions have those

Manual for the King
Commonly used for:
Array_pop,array_push,array_shift,array_unshift
Key,current,next,prev,each,end,reset,
Sort,rsort,asort,arsort,ksort,krsort
Array_diff,array_intersect,array_merge,
Array_search,array_key_exists,in_array
Array_slice,array_splice,array_rand,
Array_unique,array_keys,array_values
Count,range,list,shuffle,

http://www.bkjia.com/PHPjc/865620.html www.bkjia.com true http://www.bkjia.com/PHPjc/865620.html techarticle sorting functions in PHP Sort, asort, Rsort, Krsort, Ksort difference analysis, the asortrsort sort () function is used to sort the array cells from low to high. The Rsort () function is used to log groups of cells from ...

  • 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.