2013-03-08 71 views
1
foreach ($studentData['questions'] as $questionId => $questionData) { 



     echo '<h3>'.$questionData['questionno'].': '.$questionData['content'].'</h3>'; 

} 

上面的代碼可以顯示例如3個問題:遇到問題使用count

  1. 什麼是2 + 2?
  2. 什麼是3 + 3?
  3. 什麼是4 + 4?

現在我想要做的就是進行計數,以確定有多少問題存在,所以我做了下面的代碼:

foreach ($studentData['questions'] as $questionId => $questionData) { 

$noofquestions = count($questionData['questionno']); 

     echo '<h3>'.$questionData['questionno'].': '.$questionData['content'].'</h3>'; 

} 

但問題是,而不是爲$noofquestions輸出3是輸出1。爲什麼是這樣?

而且我想執行如何可能時間Fully Correct顯示爲$noofcorrect和seperately計數多少次Not Correct/Not Fully Correct顯示,但不知道如何用這個代碼確定此低於:

<?php 

    if($check) 
{ 
    echo '<p class="green"><strong>Fully Correct</strong></p>'; 
} 
else 
{ 
    echo '<p class="red"><strong>Not Correct/Not Fully Correct</strong></p>'; 
} 
+0

http://php.net/count – hakre 2013-03-08 05:42:57

+0

重新檢查所有的變量名。重新閱讀foreach語法文檔。我們可以看到$ studentData中有三個項目,但是您正在計數$ questionData。 – 2014-04-19 13:10:54

回答

1

做以下

$count=0; 
foreach ($studentData['questions'] as $questionId => $questionData) 
{ 

    $count += 1; 

    echo '<h3>'.$questionData['questionno'].': '.$questionData['content'].'</h3>'; 

} 

echo $count; 
0

你是在問號的字符串上調用count。爲什麼它讓你感到驚訝,它不會返回數組的數量?

foreach循環之前把$noofquestions = count($studentData['questions']);

+0

好吧,我能問如何找到正確和不正確的問題數,它是displaing同時作爲1時,它應該是2不正確的,正確的1出的三個問題 – user2048994 2013-03-08 05:45:36