我在做关于使用foreach和for循环在PHP中打印表格的LAB练习。但我现在已经遇到了这个问题。嗯......我遇到了一个未定义的错误
The question let me to print a table like this.
这里是我的代码:
<?php
$subjects = array(
"sem1" => array("Prog", "DP", "NF", "ENG", "SDD"),
"sem2" => array("IP", "DMS", "OOP", "SA"),
"sem3" => array("INSP", "SAP", "ITP"),
);
//maximum number of subjects
$maxSubNum = 10;
//creating table
echo "<table border='1'>";
//loop the array
foreach ($subjects as $sem => $subjectArray) {
//print <tr>
echo "<tr>";
//print semeester number in <td>, bold the text
echo "<td><b>$sem</b></td>\n";
//loop 10 times
for ($i=0; $i < $maxSubNum; $i++) {
//check if subject exists
if (isset($subjectArray)) {
//print subject in <td>
echo "<td>$subjectArray[$i]</td>\n";
} else {
//print empty in <td>
echo "<td></td>\n";
}
}
//closing <tr>
echo "</tr>\n";
}
echo "</table>\n";
?>
Finally, it warns me those notices although I can print out he table.
任何人都可以帮助吗?请?
而哪一行是32行? – arkascha
只需检查'isset($ subjectArray [$ i])'。您的循环迭代10次,而您的数组中的元素数量少于此数量。这就是为什么你得到undefine抵消通知 –
回声“$ subjectArray [$ i] \ n”; 这是行32 –