下面的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'是什麼......它意味着返回該索引的數組值是有問題的......但是我所要求的是造成該錯誤的原因。 –