>백엔드 개발 >PHP 튜토리얼 >php数组练习之----查询数组中某key的键值相同的个数、数组的格式转换、合并数组_PHP教程

php数组练习之----查询数组中某key的键值相同的个数、数组的格式转换、合并数组_PHP教程

WBOY
WBOY원래의
2016-07-13 17:50:06907검색

$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) {
                  

                 ["id"]=> string(1) "3"
                ["lessonid"]=> string(3) "学习课文ID"
                ["userid"]=> string(4) "6038"
                ["username"]=> string(8) "jsc11101"
                ["lessontitle"]=> string(33) "学习课文名称"
                ["courseid"]=> string(1) "4"
                ["count"]=>"学习次数"

}
}


摘自 xiaxia0083的专栏

www.bkjia.comtruehttp://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...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.