Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么这个小循环失败了?_Php_Arrays_Count_Compare_Associative Array - Fatal编程技术网

Php 为什么这个小循环失败了?

Php 为什么这个小循环失败了?,php,arrays,count,compare,associative-array,Php,Arrays,Count,Compare,Associative Array,我的循环如下: foreach( $user_answers_for_current_quiz as $question_id => $answer ) { if( $answer == $correct_answers[$question_id] ) { $score = $score + 1; } } 提供给此循环的值如下所示:- $user_answers_for_current_quiz = [ 190 => "option_2, 183 =

我的循环如下:

foreach( $user_answers_for_current_quiz as $question_id => $answer ) {
    if( $answer == $correct_answers[$question_id] ) {
        $score = $score + 1;
    }
}
提供给此循环的值如下所示:-

$user_answers_for_current_quiz = [ 190 => "option_2, 183 => "option_1"];
$correct_answers = [ 183 = "option_1", 190 => "option_2" ];
$score = 0;
我观察到的奇怪行为是,在第一次迭代中,PHP无法计算

$correct_answers[ $question_id ]
并将其计算为
NULL
。但是,在第二次迭代中,它会精确计算值并更新
$score

我查看了PHP手册,发现数组“key”可以是整数。我不确定是什么导致我的循环失败

我已经花了3个多小时试图解决这个问题,如果有人能指出正确的方向,我将不胜感激。提前感谢您抽出时间


----------------+++附录+++----------- 以下是从实际运行时复制的值:

$user_answers_for_current_quiz = [ 62 => "2", 60 => "4", 57 => "2", 54 => "4", 52 => "3" ];
$correct_answers = [ 52 => "3", 54 => "4", 62 => "2", 60 => "4", 57 => "2" ];
对于代码-

foreach( $user_answers_for_current_quiz as $question_id => $answer ) {
    if( $answer == $correct_answers[$question_id] ) {
        $score = $score + 1;
        print_r('Score'.$question_id." = ".  $score . "<br>");
    }
}
foreach($user\u answers\u作为$question\u id=>$answer用于当前的测验){
如果($answer==$correct\u answers[$question\u id]){
$score=$score+1;
打印('Score'.$question\u id.=“$Score.”,
”; } }
程序打印的内容是:
Score52=1

我发现循环无法评估“if”条件;我不知道为什么。非常感谢您的帮助

进一步编辑。从xdebug添加屏幕截图:

你的问题语法不正确。无论如何,如果没有错误的语法,代码应该可以工作。我在我的服务器上测试了这一点,似乎可以正常工作

<?php


$user_answers_for_current_quiz = [ 190 => "option_2", 183 => "option_1"];
$correct_answers = [ 183 => "option_1", 190 => "option_2" ];
$score = 0;

foreach( $user_answers_for_current_quiz as $question_id => $answer ) {
    if( $answer == $correct_answers[$question_id] ) {
       $score = $score + 1;
        print_r('Score'.$question_id." = ".  $score . "<br>");
     }
}

 print_r('Last Score: '.  $score);


?>

这是输出:


以下是您应该使用的方法:

$user_answers_for_current_quiz = [ 62 => "2", 60 => "4", 57 => "2", 54 => "4", 52 => "3" ];
$correct_answers = [ 52 => "3", 54 => "4", 62 => "2", 60 => "4", 57 => "2" ];

$score=sizeof(array_intersect_assoc($correct_answers,$user_answers_for_current_quiz));
echo $score;
// outputs: 5

array\u intersect\u assoc()
保留所有精确的键值对匹配,而不管它们在数组中的顺序/位置,
sizeof()
统计保留的元素数。这将您的方法细化为一行程序,不需要在每次迭代时有条件地覆盖/增加
$score
变量。

在选项2之后缺少一个引号<代码>190=>“选项2”…和a>在$correct中183之后回答:
183=>“选项1”…
这是您的实际代码吗?您的数组定义中似乎有一些键入错误。您缺少了一个
,然后又缺少了一个
,但在修复之后:在纠正语法错误后,它将正常工作。请检查此项,我对键入错误表示歉意。我已使用实际运行时的值更新了原始问题。但代码无法正常工作。