2013-03-08 76 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