PHP sorting function summary _ PHP Tutorial

Source: Internet
Author: User
Summary of PHP sorting functions. Sort damages index ascending value sorting Rsort damages index descending value sorting Asort keeps index ascending value sorting Arsort keeps index descending value sorting Ksort keeps index ascending key value sorting Sort destroys index ascending sort values

Rsort destroys sorting of index values in descending order

Asort sort index values in ascending order

Arsort keeps the index sorted in descending order

Ksort keeps indexes sorted by key in ascending order)

Krsort keeps the index sorted by key in descending order)

Usort

This function uses a user-defined comparison function to sort the values in an array. If the array to be sorted needs to be sorted by an unusual standard, use this function.

A comparison function must return an integer less than, equal to, or greater than zero when the first parameter is considered to be less than, equal to, or greater than the second parameter.

Uksort

Uksort-sort the keys in the array using the user-defined comparison function

Cmp_functionThe function should accept two parameters, which will beArrayIn. When the first parameter is less than, equal to, or greater than the second parameter, the comparison function must return an integer less than zero, equal to zero, or greater than zero.

Natsort

This function implements a sorting algorithm that is the same as the method that people usually sort character strings and maintain the Association of original keys/values. this is called "natural sorting ". This algorithm and the general computer string sorting algorithm

Bool natcasesort (array & $ array)Case insensitive

This function implements a sorting algorithm that is the same as the method that people usually sort character strings and maintain the Association of original keys/values. this is called "natural sorting ".

Return if successfulTRUE, Or return when the failure occurs.FALSE.

For example, to sort two-dimensional arrays, use the built-in functions asort and arsort.

The idea is to keep the key values of $ keyvalue and $ arr the same, and then assign the column $ arr wants to sort to $ keyvalue, and use the built-in function to sort $ keyvalue,

Finally, other columns of $ arr are returned based on the unchanged key value.

Function array_sort ($ arr, $ keys, $ type = 'asc ')
{
Echo 'start sorting ...'.'
';
$ Keysvalue = $ new_array = array ();
Foreach ($ arr as $ k => $ v)
{
$ Keysvalue [$ k] = $ v [$ keys];
}
If ($ type = 'asc ')
{

}
Else
{

}
Reset ($ keysvalue );
Foreach ($ keysvalue as $ k => $ v)
{
$ New_array [$ k] = $ arr [$ k];
}
Echo 'sorting ends ...'.'
';
Return $ new_array;
}

Http://www.bkjia.com/PHPjc/750880.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/750880.htmlTechArticleSort damage index ascending sort Rsort damage index descending sort Asort keep Index ascending sort Arsort keep Index descending sort Ksort keep Index ascending sort key value...

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.