This article mainly introduces the PHP array sorting code, the need for friends can refer to the following
Code below: <?php class= ' pingjiaf ' frameborder= ' 0 ' src= ' http://www.jb51.net ' scrolling= ' no ' > array sort The Usort () function sorts the array using the user-defined function. / function cmp ($a, $b)//user-defined callback function { if ($a = = $b)//If two parameters equal { return 0;/ /back to 0 } return ($a > $b) -1:1; If the 1th parameter is greater than 2nd returns 1, otherwise-1 } $a =array (3,2,5,6,1); Defines an array usort ($a, "CMP"); Use custom functions to sort the array foreach ($a as $key => $value)///Cyclic output sorted key value pairs { echo "$key: $valuen"; } /* NOTE: If two elements have the same result, 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. / //Log Group Key name sort Uksort (array,sorttype) function cmp ($a, $b)//user custom callback function { IF ($a = = $b)///If two parameters equal { return 0;//back 0 } returns ($a > $b)? -1:1; If the 1th parameter is greater than 2nd returns 1, otherwise-1 } $a =array (4=> "Four", 3 => "three", => "twenty",10=> "ten"); Define an array UKSort ($a, "CMP"); Use custom functions to sort the array of key names foreach ($a as $key => $value)///Cyclic output sorted key value pairs {//www.jbxue.com echo "$key: $valuen"; The }/* uksort () function uses a user-defined comparison function to sort by an 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. Custom functions 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"); Defines an array sort ($fruits); The array is sorted foreach ($fruits as $key => $val)////loop outputs the sorted key value pair { echo "$key = $valn";//output key value to } &NB Sp