2013-02-06 131 views
1

我的問題是我怎樣才能解決以下示例代碼中未定義的偏移錯誤?收到如何解決未知代碼中的錯誤代碼如下

誤差在屏幕上顯示:

Notice: Undefined offset: 0 in ... on line 572 Notice: Undefined offset: 0 in ... on line 572 

Question: : 

Notice: Undefined offset: 1 in ... on line 572 
Notice: Undefined offset: 1 in ...on line 572 

Question: : 

下面的代碼

 $questions = array(); 

    while ($selectedstudentanswerstmt->fetch()) { 


    $arrQuestionNo = array(); 
    $arrQuestionContent = array(); 



    $arrQuestionNo[] = $detailsQuestionNo; 
    $arrQuestionContent[] = $detailsQuestionContent; 



    $questions[] = $arrQuestionNo; 
    $questions[] = $arrQuestionContent; 

} 



<?php 

      foreach ($questions as $key=>$question) { 
//ERROR APPEARS IN LINE BELOW: 
echo '<p><strong>Question:</strong> ' .htmlspecialchars($arrQuestionNo[$key]). ': ' .htmlspecialchars($arrQuestionContent[$key]). '</p>' . PHP_EOL; 

} 
?> 

回答

0

我沒有時間來完全測試,但您嘗試訪問的問題的關鍵,但你想要得到的下一級數組的關鍵。看看這個。

<?php 

     foreach ($questions as $question) { 

     echo '<p><strong>Question:</strong> ' .htmlspecialchars($arrQuestionNo[key($question)]). ': '  .htmlspecialchars($arrQuestionContent[key($question)]). '</p>' . PHP_EOL; 

     } 
?> 

http://php.net/manual/en/function.key.php