下面的PHP代码:注意:未定义偏移:1 file.php上线33
<?php
$fopen = fopen("tasklistout.csv","r");
while(!feof($fopen))
{
$line = fgets($fopen);
echo "\r\n\t<tr>";
$piece_array = preg_split("/[\s,]+/",$line);
for ($forvar = 1; $forvar <= 5; $forvar++)
{
$array_index = $forvar - 1;
echo "\r\n\t\t<td>" . $piece_array[$array_index] . "</td>";
}
echo "\r\n\t</tr>\r\n";
}
fclose($fopen);
?>
产生以下错误:(4个分开的场合)
Notice: Undefined offset: 1 in file.php on line 33
在以下HTML文档中:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Lab_11-Objective_01--Tables</title>
<meta name="description" content="HTML 'table' element usage for Lab 11 Objective 01">
<meta name="author" content="Charles E Lentz">
<link rel="stylesheet" href="stylesheet.css">
</head>
<body>
<table>
<tr>
<th>Image Name</th>
<th>PID</th>
<th>Session Name</th>
<th>Session#</th>
<th>Mem Usage</th>
</tr>
<?php
$fopen = fopen("tasklistout.csv","r");
while(!feof($fopen))
{
$line = fgets($fopen);
echo "\r\n\t<tr>";
$piece_array = preg_split("/[\s,]+/",$line);
for ($forvar = 1; $forvar <= 5; $forvar++)
{
$array_index = $forvar - 1;
echo "\r\n\t\t<td>" . $piece_array[$array_index] . "</td>";
}
echo "\r\n\t</tr>\r\n";
}
fclose($fopen);
?>
</table>
</body>
</html>
如何解决此错误?
那么,tasklistout.csv是什么样的?我打赌一些线条没有五个由“,”分隔的值,但是只有一个。 –
在'for'循环之前使用'print_r($ piece_array)' – bystwn22
我不认为这是重复的。我知道错误'Undefined Offset'是什么......它意味着返回该索引的数组值是有问题的......但是我所要求的是造成该错误的原因。 –