Summary of PHP sort functions _php Tutorial

Source: Internet
Author: User
Sort break Index Ascending value sorting

Rsort Breaking index Descending value sort

Asort Keep Index Ascending value sort

Arsort Keep Index Descending value sort

Ksort Keep index Ascending key value sort (key)

Krsort Keep index Descending key value sort (key)

Usort

This function uses a user-defined comparison function to sort the values (value) in an array. This function should be used if the array to be sorted needs to be sorted with an unusual standard.

The comparison function must return an integer less than, equal to, or greater than 0, respectively, when the first argument is considered less than, equal to, or greater than the second argument.

Uksort

uksort-using a user-defined comparison function to sort the key names (keys) in the array

The cmp_function function should accept two parameters, which will be populated with a pair of key names in the array . The comparison function must return an integer less than 0, equal to zero, or greater than 0 when the first parameter is less than, equal to, or greater than the second argument.

Natsort

This function implements a sort algorithm that is similar to how people usually sort alphanumeric strings and maintains the association of the original key/value, which is called "natural ordering". This algorithm and the usual computer string sorting algorithm

bool Natcasesort (array & $array) Case insensitive

This function implements a sort algorithm that is similar to how people usually sort alphanumeric strings and maintains the association of the original key/value, which is called "natural ordering".

Returns TRUEon success, or FALSEon failure.

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

The idea is to keep the keys of the $keyvalue and $arr the same, and then assign the $arr to sort the column to $keyvalue, using the built-in functions to sort the $keyvalue,

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

function Array_sort ($arr, $keys, $type = ' ASC ')
{
echo ' Start sort ... '. '
';
$keysvalue = $new _array = Array ();
foreach ($arr as $k = $v)
{
$keysvalue [$k] = $v [$keys];
}
if ($type = = ' ASC ')
{
< span="">
}
Else
{
< span="">
}
Reset ($keysvalue);
foreach ($keysvalue as $k = $v)
{
$new _array[$k] = $arr [$k];
}
echo ' sort end ... '. '
';
return $new _array;
}

http://www.bkjia.com/PHPjc/750880.html www.bkjia.com true http://www.bkjia.com/PHPjc/750880.html techarticle Sort Destroy index ascending value sorts Rsort break index descending value sort Asort Keep Index ascending value sort Arsort Keep Index descending value sort Ksort Keep Index ascending key value sort ...

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