PHP multi-dimensional array sorting maintains the index correspondence and uses uasort. In actual development, you may encounter array sorting problems more or less. in addition to writing simple sorting algorithms, PHP also provides built-in array sorting functions. This article will focus on the following: uas is under development, more or less, you will encounter array sorting problems. in addition to regular write simple sorting algorithms, PHP also provides built-in array sorting functions. This article will focus on the following: uasort uses the user-defined comparison function to sort the values in the array and maintain the index Association. This function can be used to sort multi-dimensional arrays. This article focuses on this function.
Uasort function
Parameter type: bool uasort (array & $ array, callable $ cmp_function)
This function sorts arrays and maintains the association between indexes and units.
It is mainly used to sort the arrays that are very important to the unit order. Comparison functions are user-defined.
Returns TRUE if the call succeeds, or FALSE if the call fails.
Array sorting instance (not in class ):
1 /**
2 * custom sorting functions
3 * @ param $ param1
4 * @ param $ param2
5 * @ return 0 (do not move) 1 (positive exchange order)-1 (reverse exchange order)
6 */
7
8 function my_sort ($ param1, $ param2 ){
9 if ($ param1 = $ param2) return 0;
10 else return $ param1> $ param2? 1:-1;
11}
12
13 $ arr = array (
14 'a' => '20 ',
15 'B' => '1 ',
16 'C' => '10 ',
17 'd => '5 ',
18 'E' => '21 ',
19 'F' => '4 ',
20 'G' => '3 ',
21 );
22
23 uasort ($ arr, 'My _ sort ');
24
25 var_dump ($ arr );
26
27/* output value
28 array (size = 7)
29 'B' => string '1' (length = 1)
30 'G' => string '3' (length = 1)
31 'F' => string '4' (length = 1)
32 'D' => string '5' (length = 1)
33 'C' => string '10' (length = 2)
34 'a' => string '20' (length = 2)
35 'E' => string '21' (length = 2)
36 */
Multi-dimensional array sorting instance (not in class ):
/**
* Custom sorting function
* @ Param $ param1
* @ Param $ param2
* @ Return 0 (do not move) 1 (forward switching order)-1 (reverse switching order)
*/
Function my_sort ($ param1, $ param2 ){
If ($ param1 ['value'] = $ param2 ['value']) return 0;
Else return $ param1 ['value']> $ param2 ['value']? 1:-1;
}
$ Arr = array (
'A' => array ('key' => 'define 1', 'value' => '20 '),
'B' => array ('key' => 'define 2', 'value' => '1 '),
'C' => array ('key' => 'define 3', 'value' => '10 '),
'D' => array ('key' => 'define 4', 'value' => '5 '),
'E' => array ('key' => 'define 5', 'value' => '21 '),
'F' => array ('key' => 'define 6', 'value' => '4 '),
'G' => array ('key' => 'define 7', 'value' => '3 '),
);
Uasort ($ arr, 'My _ sort ');
Var_dump ($ arr );
/* Output value
Array (size = 7)
'B' =>
Array (size = 2)
'Key' => string' defines 2' (length = 7)
'Value' => string '1' (length = 1)
'G' =>
Array (size = 2)
'Key' => string 'defines 7' (length = 7)
'Value' => string '3' (length = 1)
'F' =>
Array (size = 2)
'Key' => string 'defines 6' (length = 7)
'Value' => string '4' (length = 1)
'D' =>
Array (size = 2)
'Key' => string' defines 4' (length = 7)
'Value' => string '5' (length = 1)
'C' =>
Array (size = 2)
'Key' => string' defines 3' (length = 7)
'Value' => string '10' (length = 2)
'A' =>
Array (size = 2)
'Key' => string' defines 1' (length = 7)
'Value' => string '20' (length = 2)
'E' =>
Array (size = 2)
'Key' => string 'defines 5' (length = 7)
'Value' => string '21' (length = 2)
*/
Sort in class. take a two-dimensional array as an example:
Uasort ($ arr1, array ($ this, 'public _ my_sort '));
Uasort ($ arr2, array ('self ', 'Self _ my_sort '));
Class myClassSort {
/**
* Main sorting method
* @ Param $ arr1 self static sorting
* @ Param $ arr2 this sorting
* @ Return the sorted array
*/
Public function main ($ arr1 = array (), $ arr2 = array ()){
Uasort ($ arr1, array ($ this, 'public _ my_sort '));
Uasort ($ arr2, array ('self ', 'Self _ my_sort '));
Return array ('arr1' => $ arr1, 'arr2' => $ arr2 );
}
/**
* Custom sorting function
* @ Param $ param1
* @ Param $ param2
* @ Return 0 (do not move) 1 (forward switching order)-1 (reverse switching order)
*/
Private static function self_my_sort ($ param1, $ param2 ){
If ($ param1 ['value'] = $ param2 ['value']) return 0;
Else return $ param1 ['value']> $ param2 ['value']? 1:-1;
}
// Same as above
Public function public_my_sort ($ param1, $ param2 ){
If ($ param1 ['value'] = $ param2 ['value']) return 0;
Else return $ param1 ['value']> $ param2 ['value']? 1:-1;
}
}
$ Arr = array (
'A' => array ('key' => 'define 1', 'value' => '20 '),
'B' => array ('key' => 'define 2', 'value' => '1 '),
'C' => array ('key' => 'define 3', 'value' => '10 '),
'D' => array ('key' => 'define 4', 'value' => '5 '),
'E' => array ('key' => 'define 5', 'value' => '21 '),
'F' => array ('key' => 'define 6', 'value' => '4 '),
'G' => array ('key' => 'define 7', 'value' => '3 '),
);
$ MyClassSort = new myClassSort ();
Var_dump ($ myClassSort-> main ($ arr, $ arr ));
/* The output result is the same as that of the above instance */
Function extensions
Array_multisort sorts multiple arrays or multi-dimensional arrays, but the specific one-dimensional array is used.
Arsort performs reverse sorting on one-dimensional arrays and maintains the index relationship to maintain the index correspondence.
Asort performs positive sorting on one-dimensional arrays and maintains the index relationship to maintain the index correspondence.
Krsort sorts the array in reverse order by key name to maintain the index correspondence
Ksort sorts the array in a forward order of key names to maintain the corresponding relationship between indexes.
Natcasesort uses the "natural sorting" algorithm to sort one-dimensional arrays by case-insensitive letters. it can be used to sort the combination of letters and numbers in the array content to maintain the index correspondence.
Natsort uses the "natural sorting" algorithm to sort one-dimensional arrays, which are case sensitive and can be used to sort the combination of letters and numbers in the array content to maintain the index correspondence.
Rsort sorts one-dimensional arrays in reverse order and does not maintain the index correspondence.
Sort performs positive sorting on one-dimensional arrays without maintaining the index correspondence.
Uasort uses a user-defined comparison function to sort the values in the array and maintain the index Association. This function can be used to sort multi-dimensional arrays.
Uksort uses a user-defined comparison function to sort the key names in the array
Usort uses a user-defined comparison function to sort the values in the array without retaining the index Association.
Sort also provides built-in array sorting functions. This article focuses on the following: uas...