PHP array sorting code sharing _php Tutorial

Source: Internet
Author: User
Tags array sort
Copy CodeThe code is as follows:
class= ' pingjiaf ' frameborder= ' 0 ' src= ' http://www.jb51.net ' scrolling= ' no ' >
Sort an array
The Usort () function uses a user-defined function to sort the array.
*/

function cmp ($a, $b)//user-defined callback function
{
if ($a = = $b)//If two parameters are equal
{
return 0; Returns 0
}
return ($a > $b) -1:1; If the 1th parameter is greater than 2nd, return 1, otherwise-1
}
$a =array (3,2,5,6,1); Define an array
Usort ($a, "CMP"); Use a custom function to sort an array
foreach ($a as $key + $value)//Cyclic output sorted key-value pair
{
echo "$key: $valuen";
}
/*
Note: If two elements compare the same result, their order in the sorted array is undefined. Before PHP 4.0.6, the user-defined function retains the original order of these elements. However, because of the introduction of a new sorting algorithm in 4.1.0, the result will not be this, because there is no effective solution to this.

*/

Sort an array key name Uksort (Array,sorttype)
function cmp ($a, $b)//user-defined callback function
{
if ($a = = $b)//If two parameters are equal
{
return 0; Returns 0
}
return ($a > $b) -1:1; If the 1th parameter is greater than 2nd, return 1, otherwise-1
}
$a =array (4=> "Four", 3 = "three", "Twenty",10=> "ten"); Define an array
Uksort ($a, "CMP"); Sorting the array key names using a custom function
foreach ($a as $key + $value)//Cyclic output sorted key-value pair
{//Www.jbxue.com
echo "$key: $valuen";
}/*
The Uksort () function uses a user-defined comparison function to sort the array by key name and maintain the index relationship.

Returns true if successful, otherwise false.

This function should be used if the array to be sorted needs to be sorted with an unusual standard.


The custom 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.

*/


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

Note: This function assigns the new key name to the cells in the array. The original key name is deleted.

Returns true if successful, otherwise false.

*/

$fruits =array ("Lemon", "orange", "banana", "apple"); Define an array
Sort ($fruits); Sorting an array
foreach ($fruits as $key + $val)////Loop output array after sorting the key-value pairs
{
echo "$key = $valn"; Output key-value pairs
}

http://www.bkjia.com/PHPjc/736860.html www.bkjia.com true http://www.bkjia.com/PHPjc/736860.html techarticle Copy the code as follows:? PHP class= ' pingjiaf ' frameborder= ' 0 ' src= ' http://www.jb51.net ' scrolling= ' no ' array sort usort () The function uses a user-defined function to sort the array. ...

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