PHP - 在单个变量中计算数组的两个值之间的操作差异

PHP - counting in a single variable the difference in operations between two values of arrays

我的问题是,当我计算玩家的 "wins" 时,它会计算之前值和当前值中的每场胜利。

比较详细,我有这个值

$a0 = 10;
$a1 = 12;
$a2 = 14;

$b0 = 20;
$b1 = 20; 
$b2 = 10;

$arr1 = array($a0, $a1, $a2);  // Martina's numbers 
$arr2 = array($b0, $b1, $b2);  // George's numbers


foreach($arr1 as $key => $val){

        if($val > $arr2[$key]){  // Martina win , +1 point  for Martina

            $martina++;
            print($martina . " ");

        }elseif($val < $arr2[$key]){   // George win , +1 point for George
            $george++;
            print($george . " "); 

        }else{  // if is Equal - no score increase 

            print("");

        } 

}

在这种情况下,George 的分数必须是 1 2,但我的代码还输出第一次获胜的分数,以及附加分数。

我怎样才能让它只给我增加的分数..?

希望你能理解我对问题的解释,我才刚刚开始学习这门语言。

将打印件移到 foreach

之外
$arr1 = array(10, 12, 14);  // Martina's numbers 
$arr2 = array(20, 20, 10);  // George's numbers
$alice = 0;
$bob = 0;

foreach($arr1 as $key => $val) {
    if($val > $arr2[$key]) {
        // Martina win , +1 point  for Martina
        $alice++;
    } elseif($val < $arr2[$key]) {
        // George win , +1 point for George
        $bob++;
    } 

}

print('Alice: ' . $alice . PHP_EOL);
print('Bob: ' . $bob . PHP_EOL); 

我终于做到了!!

非常感谢E_p您帮助解决了这个问题。 最终正确的代码是:

<?php

// Values of Martina's numbers

$a0 = 21;
$a1 = 2;
$a2 = 32;

//Values of George's numbers
$b0 = 22; 
$b1 = 3;  
$b2 = 13; 

//Putting the values into arrays
$arr1 = array($a0, $a1, $a2);
$arr2 = array($b0, $b1, $b2);

// Given two variables for players score
$martina = 0;
$george = 0;


foreach($arr1 as $key => $val){

            if($val > $arr2[$key]){
                $martina++;
            }elseif($val < $arr2[$key]){
                $george++;
            }

}

print($martina . " " . $george . PHP_EOL);