Php returns the maximum value after sorting two-dimensional arrays-PHP source code

Source: Internet
Author: User
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 ();
}
}
}

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.