2015-08-31 62 views
0

減小常量我試圖訪問使用的東西一個PHP數組是這樣的:PHP數組的數組索引

$ArrayName[$j-1] 

我鍵入-1的那一刻,我立即通知語法錯誤。

以這種方式在數組計數器中加/減常量是否違法?

這裏是我的代碼的一部分(跳過了SQL查詢,但查詢的工作OK):

$result=$conn->query($query); 

if(!$result) die ("Database access failed: ".$conn->error); 
$rows=$result->num_rows; 
$answerCount=3; 
$totalDataCount=0; 
while($totalDataCount<$rows){ 
for($j=$totalDataCount;$j<$answerCount;++$j){ 
    $result->data_seek($j); 
    $row=$result->fetch_array(MYSQLI_ASSOC); 
    $Answer[$j]=$row['answer_text']; 
} 
$Question4Answers=$row['Question_text']; 
echo <<<_END 

<form action="#" method="post" name="enteranswer"> 
    <table width="400"> 
    <tr> 
    <td><p>$Question4Answers</p></td> 
    </tr> 
    <tr> 
    <td><label> 
     <input type="radio" name="radioGroup1" value="answer1" > 
     $Answer[$j-1]</label></td> 
     </tr> 
     <tr> 
    <td><label> 
     <input type="radio" name="radioGroup1" value="answer2" > 
     $Answer[$j-2] </label></td> 
     </tr> 
    <tr> 
    <td><label> 
     <input type="radio" name="radioGroup1" value="answer3" > 
     $Answer[$j-3] </label></td> 
    </tr> 
    </table> 
    <input type="submit" name="radiosubmit" id="button1css" value="Submit"> 
    </form> 
    _END; 
    $totalDataCount=$totalDataCount+3; 
    $answerCount=$answerCount+3; 
    } 
} 
+0

你什麼錯誤的第二個例子? – Rizier123

+2

否,但生成數組中不存在的數組鍵是非法的,例如,負面指數。 –

+0

檢查你是否給$ j分配了一個值,使得$ j-1成爲負數 –

回答

1

沒有看到你的錯誤,這是很難做到100%,但看起來這是一個heredoc的約束。

它看起來應該是

{$Answer[$j-3]} 

這篇文章解釋

Calling PHP functions within HEREDOC strings

自PHP文檔heredoc php

+0

謝謝。你打在頭上。 – Minaj

+0

很高興聽到它。不客氣,☺ –