我的数组有问题,当数据等于数据库内的数据时,数组中的值不想添加(+1)。
有人可以帮助我,谢谢。 抱歉英语/语法不好。
案例:
A B C D E // answer
0 0 0 0 0 // count
0 1 0 0 0 // user answer B
1 1 0 0 0 // user answer A
1 2 0 0 0 // user answer B again
代码:
//create an array with values 0
$tempAns = array_fill(0,5,0);
//loop for every answered
foreach ($_POST['ques'] as $key){
//loop for every answer from DB
foreach ($_POST['ans_item'] as $value){
//if answered same with answer from DB (answer is stored inside DB)
if($key == $value)
//values in array will +1, depends on the node 0-4
$tempAns++;
}
}
答案 0 :(得分:0)
我不确定您的实施背后的逻辑是什么,但我认为以下内容可以帮助您:
$tempAns = array ( "A" => 0, "B" => 0, "C" => 0, "D" => 0, "E" => 0);
foreach(_$POST['ques'] as $key) {
$tempAns[$key]++;
}
它假定ques
包含类似的内容:
ques = {"A", "A", "B", "E", ...}
答案 1 :(得分:0)
最后经过几次尝试,结合@Pooya回答:
$size = $_POST['tempVar'] + 1;
$tempArray = array_fill(0, $size, 0);
echo "<br />";
foreach ($_POST['ques'] as $key){
$a=0;
foreach ($ans_item as $value){
if($key == $value){
$tempArray[$a]++;
}
$a++;
}
}
谢谢@Pooya!
也感谢那些低估我的帖子的人!