CodeIgniter PHP生成Excel显示数据库数据遇undefined offset错误求解决
问题描述
尝试将数据库数据导出到Excel表格时,触发了undefined offset错误,错误出现在以enrollmentID为标识键的foreach循环中,相关代码如下:
if($key == 'enrollmentID'){ $intLec = 0; $intLab = 0; foreach ($enrollmentSubjectListData[$value]['subjectCode'] as $subjKey => $subjValue) { $coordinates = $this->getExcelColumnConversion($col) . $row; $this->excel->getActiveSheet()->setCellValue($coordinates, $subjValue); $col++; $coordinates = $this->getExcelColumnConversion($col) . $row; $this->excel->getActiveSheet()->setCellValue($coordinates, $enrollmentSubjectListData[$value]['numericalEquivalent'][$subjKey]); $col++; if($enrollmentSubjectListData[$value]['lab'][$subjKey] != 0) $units = $enrollmentSubjectListData[$value]['lec'][$subjKey] . '/' . $enrollmentSubjectListData[$value]['lab'][$subjKey]; else $units = $enrollmentSubjectListData[$value]['lec'][$subjKey]; $coordinates = $this->getExcelColumnConversion($col) . $row; $this->excel->getActiveSheet()->setCellValue($coordinates, $units); $col++; $intLec += (float)str_replace(array( '(', ')' ), '', $enrollmentSubjectListData[$value]['lec'][$subjKey]); $intLab += (float)$enrollmentSubjectListData[$value]['lab'][$subjKey]; } if($intLab == 0){ $coordinates = $this->getExcelColumnConversion($col) . $row; $this->excel->getActiveSheet()->setCellValue($coordinates, $intLec); } else{ $coordinates = $this->getExcelColumnConversion($col) . $row; $this->excel->getActiveSheet()->setCellValue($coordinates, $intLec . '/' . $intLab); } }
错误原因与修复方案
错误原因
undefined offset错误的核心是:通过$subjKey访问numericalEquivalent、lec、lab子数组时,该键不存在。常见场景包括:
subjectCode数组的键与其他子数组的键不匹配(比如一个是关联键,另一个是索引键)- 部分子数组缺失对应
$subjKey的元素 enrollmentSubjectListData[$value]本身未包含某个必要的子数组(比如numericalEquivalent不存在)
修复步骤
- 前置校验:进入foreach循环前,先验证当前
enrollmentID对应的所有必要子数组是否存在,避免访问不存在的数组 - 元素存在性检查:循环中访问子数组元素时,用
isset()或??运算符确保键存在,避免直接访问不存在的偏移量 - 统一数据处理:对学分值做默认值兜底,确保累加计算不会出错
修复后的代码
if($key == 'enrollmentID'){ $intLec = 0; $intLab = 0; // 前置检查:确保当前enrollmentID对应的所有必要数组存在 if(!isset($enrollmentSubjectListData[$value]['subjectCode'], $enrollmentSubjectListData[$value]['numericalEquivalent'], $enrollmentSubjectListData[$value]['lec'], $enrollmentSubjectListData[$value]['lab'])){ // 数据不完整,跳过当前行或做日志记录 continue; } $subjectCodes = $enrollmentSubjectListData[$value]['subjectCode']; $numericalEquivs = $enrollmentSubjectListData[$value]['numericalEquivalent']; $lecUnits = $enrollmentSubjectListData[$value]['lec']; $labUnits = $enrollmentSubjectListData[$value]['lab']; foreach ($subjectCodes as $subjKey => $subjValue) { // 写入科目代码 $coordinates = $this->getExcelColumnConversion($col) . $row; $this->excel->getActiveSheet()->setCellValue($coordinates, $subjValue); $col++; // 写入数值等效值,不存在则设为空 $numValue = isset($numericalEquivs[$subjKey]) ? $numericalEquivs[$subjKey] : ''; $coordinates = $this->getExcelColumnConversion($col) . $row; $this->excel->getActiveSheet()->setCellValue($coordinates, $numValue); $col++; // 处理学分,确保lec和lab的键存在,默认值设为0 $lec = isset($lecUnits[$subjKey]) ? str_replace(array( '(', ')' ), '', $lecUnits[$subjKey]) : '0'; $lab = isset($labUnits[$subjKey]) ? $labUnits[$subjKey] : '0'; $units = $lab != 0 ? $lec . '/' . $lab : $lec; $coordinates = $this->getExcelColumnConversion($col) . $row; $this->excel->getActiveSheet()->setCellValue($coordinates, $units); $col++; // 累加学分,确保转换为浮点数 $intLec += (float)$lec; $intLab += (float)$lab; } // 写入总学分 $totalUnits = $intLab == 0 ? $intLec : $intLec . '/' . $intLab; $coordinates = $this->getExcelColumnConversion($col) . $row; $this->excel->getActiveSheet()->setCellValue($coordinates, $totalUnits); }
内容的提问来源于stack exchange,提问作者jovlog
相关产品推荐
相关产品推荐

