PHP sorting functions sort, asort, rsort, krsort, and ksort differences analysis, asortrsort_PHP tutorial

Source: Internet
Author: User
The sorting functions sort, asort, rsort, krsort, and ksort in PHP are differentiated analysis and asortrsort. The sorting functions sort, asort, rsort, krsort, and ksort in PHP perform difference analysis. the asortrsortsort () function is used to sort array units from low to high. The rsort () function is used to analyze the differences among the sort functions sort, asort, rsort, krsort, and ksort of array units in PHP. asortrsort

The sort () function is used to sort array units from low to high.
The rsort () function is used to sort array units from high to low.
The asort () function is used to sort array units from low to high and maintain the index relationship.
Arsort () is used to sort array units from high to low and maintain the index relationship.
The ksort () function is used to sort array units by key names from low to high.
The krsort () function is used to sort array units by key names from high to low.

Sort ()

The PHP sort () function is used to sort array units from low to high. If the array unit is successfully sorted, TRUE is returned. If the array unit fails, FALSE is returned.
Note: This function will assign a new key name to the unit in the sorted array, which will delete the original key name, not just the reorder.
Syntax:
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
The SORT_NUMERIC unit is compared as a number.
The SORT_STRING unit is compared as a string.
SORT_LOCALE_STRING compare the unit as a string based on the current region (locale) settings

Example:

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

Run the sample output:

  Array ( [0] => a [1] => b [2] => c )

In this example, the $ arr array units are sorted alphabetically, and the array units are sorted and the key values are re-allocated.

Rsort ()
The PHP rsort () function is opposite to sort (). sort the array units in descending order. For more information, see sort () function.

Asort ()
The PHP asort () function is used to sort array units from low to high and maintain the index relationship. if it succeeds, TRUE is returned, and FALSE is returned if it fails.
Syntax:
Bool asort (array & array [, int sort_flags])
The optional parameter sort_flags is used to change the sorting behavior. for details, see sort ().
Example:
  

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

Run the sample output:

  Array ( [1] => a [0] => b [2] => c )

Arsort ()
The PHP arsort () function is opposite to asort (). It sorts array units from high to low and maintains the index relationship. For more information, see use the asort () function.

Ksort ()
The PHP ksort () function is used to sort array cells by key names from low to high. if the key name is successful, TRUE is returned. if the key name fails, FALSE is returned.
This function retains the original key name, so it is often used to associate arrays.
Syntax:
Bool ksort (array & array [, int sort_flags])
The optional parameter sort_flags is used to change the sorting behavior. for details, see sort ().
Example:
  
  

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

Run the sample output:

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

  Krsort ()
The behavior of the PHP krsort () function is opposite to that of ksort (). sort the array units by key name from high to low. for details, refer to the ksort () function.


Characteristics of various sorting in php

Ignore sorting of key names
Sort ()
Rsort ()
Usort ()
Sort the reserved key names
Asort ()
Arsort ()
Uasort ()
Sort by key name
Ksort ()
Krsort ()
Uksort ()
Sort by natural number
Uatsort ()
Uatcasesort ()
Random Sorting
Shuffle ()
Reverse sort to new array
Array_reverse ()

There are too many things. you can only give you the function name. please refer to the manual for details.

Php array functions

The manual is king
Common:
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,

Sort () is used to sort array units from low to high. The rsort () function is used to set the array unit 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.