Home >Backend Development >PHP Tutorial >PHP array exercise--query the number of the same key value of a certain key in the array, format conversion of the array, and merge arrays_PHP tutorial

PHP array exercise--query the number of the same key value of a certain key in the array, format conversion of the array, and merge arrays_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:50:06907browse

$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)
                   。。。。。。。。。。。。。。。。。。。。。。。。
}


相同lessonid的学习次数

$study_num = array();

foreach($array as $value){

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

                                                                                  //如果$study_num[$value['lessonid']为第一次遍   历赋值为空

}

学习次数:   $study_num[$lessionid]

转换成以lessonid为key的数组

$lessonid_array = array();

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

                 $lessonid_array[$value[lessonid]]=$value;

}

学习课文名称:  $lessonid_array[$lessonid]['lessontitle']

合并数组

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

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

}

 输出数组形式如下
               

$a_array  = array(){              
          lessonid号 => array(7) {
                  

                                                                                                                                                                                                                                                                                           Since                    ["lessonid"]=> string(3) "Learning text ID"
                                                                              ["userid"]=> string(4) "6038"
                                                                           ["username"]=> string(8) "jsc11101"
                                                                                                                                                                                                                   since                                                     through           ["lessontitle"]=>                    ["courseid"]=> string(1) "4"
                                             ["count"]=>"Number of learning times"

}

}


Excerpted from xiaxia0083’s column



http://www.bkjia.com/PHPjc/478290.html

truehttp: //www.bkjia.com/PHPjc/478290.htmlTechArticle$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 Im Fr...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn