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

Source: Internet
Author: User
Php array sorting usort, uksort and sort function usage, usortuksort. Php array sorting usort, uksort, and sort functions. usortuksort this article describes the usage of php array sorting usort, uksort, and sort functions. Share it with you for your reference. The specific usage is divided into php array sorting usort, uksort and sort functions, and usortuksort

This document describes the usage of php array sorting functions (usort, uksort, and sort. Share it with you for your reference. The usage analysis is as follows:

Sort arrays: the usort () function uses a user-defined function to sort arrays. the instance code is as follows:

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 )? -; // If the number of 1st parameters is greater than 2nd, 1 is returned. otherwise,-1 is returned.
}
$ A = array (3, 2, 5, 6, 1); // defines an array
Usort ($ a, "cmp"); // use a UDF to sort arrays.
Foreach ($ a as $ key => $ value) // cyclically outputs sorted key-value pairs
{
Echo "$ key: $ valuen ";
}


Note: If the comparison results of the two elements are the same, their order in the sorted array is not defined. before php 4.0.6, the user-defined function retains the original order of these elements, however, because a new sorting algorithm is introduced in 4.1.0, this is not the case because there is no effective solution.

Sort the array key names by uksort (array, sorttype). The instance code is as follows:

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 )? -; // If the number of 1st parameters is greater than 2nd, 1 is returned. otherwise,-1 is returned.
}
$ A = array (4 => "four", 3 => "three", 20 => "twenty", 10 => "ten"); // define an array
Uksort ($ a, "cmp"); // use a custom function to sort the array key names
Foreach ($ a as $ key => $ value) // cyclically outputs sorted key-value pairs
{
Echo "$ key: $ valuen ";
}


The uksort () function uses the custom comparison function to sort the array by key name and maintain the index relationship. if the result is successful, true is returned. otherwise, false is returned.

If the array to be sorted needs to be sorted by an unusual standard, this function should be used. the custom function should accept two parameters, this parameter will be filled by a pair of key names in the array. when the first parameter is smaller than, equal to, or greater than the second parameter, the comparison function must return a value smaller than or equal to zero, or an integer greater than zero.

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. the original key name is deleted. if the function is successful, true is returned. otherwise, false is returned. the instance code is as follows:

The code is as follows:

$ Fruits = array ("lemon", "orange", "banana", "apple"); // defines an array
Sort ($ fruits); // sorts the array
Foreach ($ fruits as $ key => $ val) // cyclically output the sorted key-value pair of the array
{
Echo "$ key = $ valn"; // output key-value pairs
}

I hope this article will help you with PHP programming.

Examples in this article describes the usage of php array sorting usort, uksort, and sort functions. Share it with you for your reference. Usage...

Related Article

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.