2013-08-24 47 views
-1

下面的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> 

如何解決此錯誤?

+4

那麼,tasklistout.csv是什麼樣的?我打賭一些線條沒有五個由「,」分隔的值,但是隻有一個。 –

+0

在'for'循環之前使用'print_r($ piece_array)' – bystwn22

+0

我不認爲這是重複的。我知道錯誤'Undefined Offset'是什麼......它意味着返回該索引的數組值是有問題的......但是我所要求的是造成該錯誤的原因。 –

回答

1

Undefined offset錯誤表示該變量中的數組項不存在。這表明這個問題在你的代碼中應該被創建(但不是)的地方。例如,preg_split("/[\s,]+/",$line);行可能是這裏的問題。

要找出加入這一行:如果您需要

var_dump($piece_array);

此行

$piece_array = preg_split("/[\s,]+/",$line);

後futher幫助後的結果作爲一個編輯,我會盡力幫助你。

+0

這是一個非常愚蠢的錯誤。該錯誤甚至沒有在PHP中,它在csv文件中......底部有1行,一個額外的空白行引發這個錯誤.....我應該刪除這個問題嗎? –

0

至於建議的Sergiu,.csv文件可能是不格式化,以包含五個逗號(也許只有一個)

你應該嘗試使用foreach loop,而不是實現這一目標,從而避免了數組索引(嗯,有點)。 但首先var_dump$piece_array,看看它不是空的,否則你可能要檢查.csv文件

1

。在你的csv文件在最後一個空行。
使用issetempty檢查索引是否存在於您的數組中。
取代while循環,您可以使用foreach循環以及file函數執行循環。看我的示例代碼。

<?php 
    // read entire file into an array 
    $lines = file("tasklistout.csv"); 
    // loop through each line 
    foreach($lines as $line) { 
    // remove whitespace from line 
    $line = trim($line); 
    // make sure that line is not empty 
    if ($line) { 
     // split line with comma or space 
     $piece_array = preg_split("/[\s,]+/", $line); 
     // make sure that array contains at least 1 value 
     if (!empty($piece_array)) { 
     echo "\r\n\t<tr>"; 
     for($i = 0; $i < 5; $i++) { 
      if (isset($piece_array[$i])) { 
      echo "\r\n\t\t<td>".$piece_array[$i]."</td>"; 
      } 
      else { 
      echo "\r\n\t\t<td>&nbsp;</td>"; 
      } 
     } 
     echo "\r\n\t</tr>\r\n"; 
     } 
    } 
    } 
?> 
+0

'FILE_SKIP_EMPTY_LINES'更簡單。 – mario