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
}