Home  >  Article  >  Backend Development  >  Summary of PHP sorting functions_PHP tutorial

Summary of PHP sorting functions_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:34:31744browse

Sort Destroy index Ascending value sort

Rsort Destroy Index Descending Value Sort

Asort Keep index ascending order Value sort

Arsort Keep index in descending order Value sort

Ksort Keep the index in ascending order Key value sorting (key)

Krsort Keep index descending order Key value sorting (key)

Usort

This function will use a user-defined comparison function to sort the values ​​in an array. This function should be used if the array to be sorted needs to be sorted by an unusual criterion.

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

Uksort

uksort — Sort keys in an array using a user-defined comparison function

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

Natsort

This function implements a sorting algorithm in the same way that people usually sort alphanumeric strings and maintains the original key/value association. This is called "natural sorting". This algorithm is the same as the usual computer string sorting algorithm

bool natcasesort (array &$array)Case-insensitive

This function implements a sorting algorithm in the same way that people usually sort alphanumeric strings and maintains the original key/value association. This is called "natural sorting".

Returns TRUE on success, or FALSE.

on failure

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

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

Finally, return other columns of $arr based on the unchanged key values.

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;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/750880.htmlTechArticleSort Destroy index ascending value sort Rsor Destroy index descending value sort Asort Keep index ascending value sort Arsort Keep index descending value sort Ksort keeps the index sorted by key values ​​in ascending order...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn