Php array exercise ---- query the number of keys with the same key value in the array, convert the array format, and merge Arrays

Source: Internet
Author: User

$ Array = array (){

[0] => array (6 ){
["Id"] => string (1) "2"
["Lessonid"] => string (3) "200"
["Userid"] => string (4) "6038"
["Username"] => string (8) "jsc11101"
["Lessontitle"] => string (47) "Module1-Unit2 I'm From China"
["Courseid"] => string (1) "4"
}

[1] => array (6 ){
["Id"] => string (1) "3"
["Lessonid"] => string (3) "201"
["Userid"] => string (4) "6038"
["Username"] => string (8) "jsc11101"
["Lessontitle"] => string (33) "Module2-Unit1 I Can Speak English"
["Courseid"] => string (1) "4"
}
[5] => array (6)
........................
}


Number of times of learning with the same lessonid

$ Study_num = array ();

Foreach ($ array as $ value ){

$ Study_num [$ value ['lesonid'] = (empty ($ study_num [$ value ['lesonid'])? 0: $ study_num1 [$ value ['lesonid']) + 1;

// If $ study_num [$ value ['lesonid'] is null for the first calendar

}

Number of times: $ study_num [$ lessionid]

Convert to an array with lessonid as the key

$ Lessonid_array = array ();

Foreach ($ array as $ key => $ value ){

$ Lessonid_array [$ value [lessonid] = $ value;

}

Learning text name: $ lessonid_array [$ lessonid] ['lesontitle']

Merge Arrays

Foreach ($ study_num as $ key => $ value ){

$ Lessonid_array [$ key] [count] = $ value;

}

The output array format is as follows:

$ A_array = array (){
Lessonid => array (7 ){

["Id"] => string (1) "3"
["Lessonid"] => string (3) "Learning text ID"
["Userid"] => string (4) "6038"
["Username"] => string (8) "jsc11101"
["Lessontitle"] => string (33) "Learning text name"
["Courseid"] => string (1) "4"
["Count"] => "learning times"

}
}


From the column xiaxia0083

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.