You can use functions such as sort (), asort (), and arsort () to sort PHP one-dimensional arrays. However, you need to customize the sorting of PHP two-dimensional arrays. You can use functions such as sort (), asort (), and arsort () to sort PHP one-dimensional arrays. However, you need to customize the sorting of PHP two-dimensional arrays.
Script ec (2); script
Custom:
| The Code is as follows: |
|
|
Function array_sort ($ arr, $ keys, $ type = 'asc ') {// 2-dimensional array sorting
$ Keysvalue = $ new_array = array ();
Foreach ($ arr as $ k =>$ v ){
$ Keysvalue [$ k] = $ v [$ keys];
}
If ($ type = 'asc '){
Asort ($ keysvalue );
} Else {
Arsort ($ keysvalue );
}
Reset ($ keysvalue );
Foreach ($ keysvalue as $ k => $ v ){
$ New_array [$ k] = $ arr [$ k];
}
Return $ new_array;
} |
Recently, a data analysis system needs to use various sorting methods. Below is a sorting process:
| The Code is as follows: |
|
|
// $ Re is the data source, two-dimensional array
$ Re = $ this-> array_sort ($ re, 'lv', 'desc'); // sort
Foreach ($ re as $ key => $ val ){
Foreach ($ val as $ key2 => $ val2 ){
If ($ key2 = "uid "){
$ Count [] = $ val2; // obtain the total number of created roles
$ Lv [$ val2] [] = $ re [$ key] ['lv']; // obtain all role levels
}
}
}
$ Count = array_count_values ($ count );
Foreach ($ count as $ key => $ val ){
M ()-> query ("update mj_ad_count set create_juese = create_juese +". $ val. "where suid =". $ key );
Echo M ()-> getLastSql ();
}
Foreach ($ lv as $ key => $ val) {// level check to find the maximum value and write statistics
Foreach ($ val as $ key2 => $ val2 ){
If ($ key2 = 0 ){
// $ Ls [$ key] [] = $ val2;
M ()-> query ("update mj_ad_count set juese_leave =". $ val2. "where suid =". $ key );
Echo M ()-> getLastSql ();
}
}
} |