我尝试将数据库中的值显示到Excel工作表中,问题是我使用的是undefined offset error
问题从这里开始:
代码:enrollmentID
是我识别每个值的键,foreach是未定义偏移量的问题。有没有办法解决这个问题,这样我就可以在excel中显示我的数据了?
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);
}
}
1条答案
按热度按时间zysjyyx41#
undefined offset
错误通常意味着您试图在数组中检索的键不存在。在这里,如果在
foreach
语句中抛出错误,原因可能是:$enrollmentSubjectListData
数组中不存在$value键$enrollmentSubjectListData[$value]
不包含'subjectCode'
键(或者$enrollmentSubjectListData[$value]
不是数组)您可以实现的一个简单修复方法是用
IF
语句包围foreach
:请记住,这段代码只会防止代码崩溃,但它不会告诉您为什么输入数据最初没有正确格式化。
为此,我建议您在IDE中使用xDebug之类的调试器。