检查得分高的数组点


checking array point with high score

我的意图是检查一个数组,我会将其中一个点设置为高分,如果数组中任何其他3个点与高分相同,则echo有2个相同点,否则echo不相同。所以基本上是检查所有点,任何具有相同点的点。

我的代码

$Dpoint =70;
$Ipoint =70;
$Hpoint =60;
$Apoint =60;
$score = $Dpoint; high score
$xdata = array(
            'D1' => $Dpoint,
            'I1' => $Ipoint,
            'H1' => $Hpoint,
            'A1' => $Apoint
            );
    foreach($xdata as $key => $value){
    if($score == $value){
    echo "there is 2 same point";
    }else{
    echo "not the same";
    }
}

但是我得到了werid的结果,我错过了什么部分?修复我的

如果我很了解你。。。

<?php
    $Dpoint =70;
    $Ipoint =70;
    $Hpoint =60;
    $Apoint =60;
    $score = $Dpoint;
    $xdata = array(
                   'D1' => $Dpoint,
                   'I1' => $Ipoint,
                   'H1' => $Hpoint,
                   'A1' => $Apoint
                   );
    $matches = 0;
    foreach($xdata as $key => $value){
        if($score == $value){
            $matches ++;
        }
    }
    echo $matches > 0 ? "$matches same point(s)": "no same points";
?>

如注释所示:

我可以自己跳过$Dpoint检查吗?我只想让它呼应"不一样"——巴巴亚加5分钟前的

如果你不想让高分出现在你的循环中,只需unset(),它和你可以用array_search()获得的密钥:

unset($xdata[array_search($score, $xdata)]);