Home > Backend Development > PHP Tutorial > PHP array sorting usort, uksort and sort function usage, usortuksort_PHP tutorial

PHP array sorting usort, uksort and sort function usage, usortuksort_PHP tutorial

WBOY
Release: 2016-07-13 10:13:53
Original
1688 people have browsed it

PHP array sorting usort, uksort and sort function usage, usortuksort

The examples in this article describe the usage of php array sorting usort, uksort and sort functions. Share it with everyone for your reference. The specific usage analysis is as follows:

Sort the array: The usort() function uses a user-defined function to sort the array. The example code is as follows:

Copy code The code is as follows:
function cmp($a, $b) //User-defined callback function
{
if($a==$b) //If the two parameters are equal
{
Return 0; //Return 0
}
Return($a>$b)?-1:1; //If the first parameter is greater than the second parameter, return 1, otherwise -1
}
$a=array(3,2,5,6,1); //Define an array
usort ($a,"cmp"); //Use a custom function to sort the array
foreach($a as $key=>$value) //Loop to output the sorted key-value pairs
{
echo "$key:$valuen";
}

Note: If the comparison results of two elements are the same, their order in the sorted array is undefined. Before PHP 4.0.6, the user-defined function will retain the original order of these elements, but since 4.1.0 With the introduction of a new sorting algorithm in , the result will no longer be like this because there is no effective solution for this.

Sort the array keys using uksort(array, sorttype), the example code is as follows:

Copy code The code is as follows:
function cmp($a, $b) //User-defined callback function
{
if($a==$b) //If the two parameters are equal
{
Return 0; //Return 0
}
Return($a>$b)?-1:1; //If the first parameter is greater than the second parameter, return 1, otherwise -1
}
$a=array(4=>"four",3 =>"three",20 =>"twenty",10=>"ten"); //Define an array
uksort ($a,"cmp"); //Use a custom function to sort the array keys
foreach($a as $key=>$value) //Loop to output the sorted key-value pairs
{
echo "$key:$valuen";
}

The uksort() function uses a user-defined comparison function to sort the array by key name and maintain the index relationship. If successful, it returns true, otherwise it returns false.

If the array to be sorted needs to be sorted by an unusual criterion, this function should be used. The custom function should accept two parameters, which will be filled with a pair of key names in the array. The comparison function is in When the first parameter is less than, equal to, or greater than the second parameter, an integer less than zero, equal to zero, or greater than zero must be returned respectively.

The sort() function sorts the values ​​of the given array in ascending order.

Note: This function assigns a new key name to the unit in the array, and the original key name will be deleted. If successful, it returns true, otherwise it returns false. The example code is as follows:

Copy code The code is as follows:
$fruits=array("lemon","orange","banana","apple"); // Define an array
sort($fruits); //Sort the array
foreach($fruits as $key=>$val) //Loop to output the sorted key-value pairs of the array
{
echo "$key=$valn"; //Output key-value pairs
}

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/912674.htmlTechArticlephp array sorting usort, uksort and sort function usage, usortuksort This article explains the php array sorting usort, uksort and sort with examples Function usage. Share it with everyone for your reference. The specific usage is divided into...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template