PHP array sort usort, uksort and sort function _php tips

Source: Internet
Author: User
Tags array sort sorts

This article describes the PHP array sort usort, uksort and sort function usage. Share to everyone for your reference. The specific usage analysis is as follows:

Array sorting: the Usort () function sorts the array of user-defined functions, with the instance code as follows:

Copy Code code as follows:
function cmp ($a, $b)//user Custom callback function
{
if ($a = = $b)//If two parameters are equal
{
return 0; return 0
}
return ($a > $b) -1:1; If the 1th argument is greater than the 2nd one returns 1, otherwise-1
}
$a =array (3,2,5,6,1); Define an array
Usort ($a, "CMP"); Sorting by using custom functions
foreach ($a as $key => $value)//Cyclic output sorted key value pairs
{
echo "$key: $valuen";
}

Note: If the two elements are the same, the order in the sorted array is undefined, and before PHP 4.0.6, the user-defined function retains the original order of the elements, but as a result of the introduction of a new sorting algorithm in 4.1.0, it will not be the case. Because there is no effective solution to this.

The array key names are sorted Uksort (Array,sorttype), and the instance code is as follows:

Copy Code code as follows:
function cmp ($a, $b)//user Custom callback function
{
if ($a = = $b)//If two parameters are equal
{
return 0; return 0
}
return ($a > $b) -1:1; If the 1th argument is greater than the 2nd one returns 1, otherwise-1
}
$a =array (4=> "Four", 3 => "three", => "twenty",10=> "ten"); Define an array
Uksort ($a, "CMP"); Using a custom function to sort the array of key names
foreach ($a as $key => $value)//Cyclic output sorted key value pairs
{
echo "$key: $valuen";
}

The Uksort () function uses a user-defined comparison function to sort by an array of key names, maintains an indexed relationship, returns true if successful, or returns false.

If the array to be sorted needs to be sorted with an unusual standard, you should use this function, the custom function should accept two parameters, which will be filled by a pair of key names in the array, the comparison function in the first argument is less than, equal to, or greater than the second parameter must return a difference of less than 0, equal to zero, Or an integer greater than 0.

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

Note: This function assigns a new key name to the cell in the array, the original key is deleted, returns true if successful, or false, and the instance code is as follows:

Copy Code code as follows:
$fruits =array ("Lemon", "orange", "banana", "apple"); Define an array
Sort ($fruits); To sort a set of arrays
foreach ($fruits as $key => $val)//Cyclic output the key value pairs after the sort of the array
{
echo "$key = $valn"; Output key value pairs
}

I hope this article will help you with your PHP program design.

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.