做php导出,需要循环输出数据到execl中,一开始写了一个,觉得时间有点长,就缩短了下,发现时间还变多了,求教这是为什么啊?谢谢了先。
原先的代码片段:
//归总每个一级分类下的二级分类
foreach($first_cate_array as $fkey => $fvalue){
foreach($cate as $ck=>$cv){
if($cv['parentid'] == $fvalue['id']){
$first_cate_array[$fkey]['second_cate'][] = $cv;
}
}
//获得每个一级分类下二级分类的数目
$second_count = count($first_cate_array[$fkey]['second_cate']);
$first_cate_array[$fkey]['second_count'] = $second_count == 0 ? 1 : $second_count;
}
/*************************************************************************/
foreach($first_cate_array as $fk => $fv){
//一级目录起始行数
if($fk == 0){
$stapos = $num;
$en_stapos = $stapos+$cate_count;
}else{
$stapos = $stapos+$first_cate_array[$fk-1]['second_count'];
$en_stapos = $stapos+$cate_count;
}
foreach($fv['second_cate'] as $sck => $scv){
//二级目录起始行数
$stapos1 = $stapos+$sck;
$en_stapos1 = $en_stapos+$sck;
foreach($scv['pro'] as $spk => $spv){
$str1 = 70+$spk*3;
$str1 = chr($str1);
$str2 = 71+$spk*3;
$str2 = chr($str2);
$str3 = 72+$spk*3;
$str3 = chr($str3);
//中文
$collect = $spv['name'].'('.$spv['description'].')';
$objActSheet->setCellValue($str1.$stapos1, $collect);
$objActSheet->setCellValue($str2.$stapos1, $spv['name']);
$objActSheet->setCellValue($str3.$stapos1, $spv['description']);
//英文
$en_collect = $spv['en_name'].'('.$spv['en_description'].')';
$objActSheet->setCellValue($str1.$en_stapos1, $en_collect);
$objActSheet->setCellValue($str2.$en_stapos1, $spv['en_name']);
$objActSheet->setCellValue($str3.$en_stapos1, $spv['en_description']);
}
//中文
$objActSheet->setCellValue('E'.$stapos1, $scv['category']);
//英文
$objActSheet->setCellValue('E'.$en_stapos1, $scv['en_category']);
}
//中文
$objActSheet->setCellValue('D'.$stapos, $fv['category']);
$stopos = $stapos + $fv['second_count'] - 1;
$mergecellstr = 'D'.$stapos.':D'.$stopos;
$objActSheet->mergeCells($mergecellstr);
//英文
$objActSheet->setCellValue('D'.$en_stapos, $fv['en_category']);
$en_stopos = $en_stapos + $fv['second_count'] - 1;
$en_mergecellstr = 'D'.$en_stapos.':D'.$en_stopos;
$objActSheet->mergeCells($en_mergecellstr);
}
修改以后的片段:
$second_count = 0;
//归总每个一级分类下的二级分类
foreach($first_cate_array as $fkey => $fvalue){
//一级目录起始行数
$stapos = $num+$second_count;
$en_stapos = $stapos+$cate_count;
$second_stapos = 0;
foreach($cate as $ck=>$cv){
if($cv['parentid'] == $fvalue['id']){
//二级目录起始行数
$stapos1 = $stapos+$second_stapos;
$en_stapos1 = $en_stapos+$second_stapos;
foreach($cv['pro'] as $spk => $spv){
$str1 = 70+$spk*3;
$str1 = chr($str1);
$str2 = 71+$spk*3;
$str2 = chr($str2);
$str3 = 72+$spk*3;
$str3 = chr($str3);
//中文
$collect = $spv['name'].'('.$spv['description'].')';
$objActSheet->setCellValue($str1.$stapos1, $collect);
$objActSheet->setCellValue($str2.$stapos1, $spv['name']);
$objActSheet->setCellValue($str3.$stapos1, $spv['description']);
//英文
$en_collect = $spv['en_name'].'('.$spv['en_description'].')';
$objActSheet->setCellValue($str1.$en_stapos1, $en_collect);
$objActSheet->setCellValue($str2.$en_stapos1, $spv['en_name']);
$objActSheet->setCellValue($str3.$en_stapos1, $spv['en_description']);
}
//中文
$objActSheet->setCellValue('E'.$stapos1, $cv['category']);
//英文
$objActSheet->setCellValue('E'.$en_stapos1, $cv['en_category']);
$second_stapos ++ ;
$second_count ++ ;
}
}
//中文
$objActSheet->setCellValue('D'.$stapos, $fvalue['category']);
$stopos = $num + $second_count - 1;
$mergecellstr = 'D'.$stapos.':D'.$stopos;
$objActSheet->mergeCells($mergecellstr);
//英文
$objActSheet->setCellValue('D'.$en_stapos, $fvalue['en_category']);
$en_stopos = $num + $cate_count + $second_count - 1;
$en_mergecellstr = 'D'.$en_stapos.':D'.$en_stopos;
$objActSheet->mergeCells($en_mergecellstr);
}
我觉得原先的代码多一个上面的循环,时间应该多一点。查询1700条数据耗时84s。
foreach的多少不是影响性能的主因,而你foreach很多次一个耗时很长的代码就会直接增加运行时间。
你修改代码后,好像遍历次数还比之前多了,你要检查程序的嵌套及循环体的合理性。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。