PHP Log Group sort code sharing _php instance

Source: Internet
Author: User
Tags sorts
Copy Code code as follows:

<?php
class= ' pingjiaf ' frameborder= ' 0 ' src= ' http://www.jb51.net ' scrolling= ' no ' >
sorting arrays
The Usort () function sorts the array using the user-defined function.
*/

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 compare the same results, their order in the sorted array is undefined. User-defined functions will retain the original order of these elements before 4.0.6 to PHP. But as a result of the introduction of a new sorting algorithm in 4.1.0, this is not the case because there is no effective solution to this.

*/

Logarithmic group key name ordering Uksort (array,sorttype)
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
{//Www.jb51.net
echo "$key: $valuen";
}/*
The Uksort () function uses a user-defined comparison function to sort the array of key names and maintains an indexed relationship.

Returns true if successful, otherwise returns false.

If the array you want to sort needs to be sorted with an unusual standard, you should use this function.


The custom function should accept two parameters, which will be populated by 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 argument 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 a new key name to the cell in the array. The original key name was deleted.

Returns true if successful, otherwise returns false.

*/

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

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.